Ask Your Question
2

How can I use querySelectorAll to locate a child element without selecting its grandchild element?

asked 2023-05-28 21:50:24 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-05-28 22:00:02 +0000

plato gravatar image

You can use the direct child selector (>) to only select the immediate children of an element. For example, if you want to select all the

  • elements inside a
      element without selecting any nested
        elements, you can use the following code:

        const listItems = document.querySelectorAll("ul > li");
        

        This will only select the

      • elements that are direct children of
          elements and not any nested
            elements.

  • edit flag offensive delete link more

    Your Answer

    Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

    Add Answer


    Question Tools

    Stats

    Asked: 2023-05-28 21:50:24 +0000

    Seen: 12 times

    Last updated: May 28 '23