Search code examples
javascript

querySelector child to parent


Nowadays I am working on an application. I been struggling for a while how I should select a parent of a child.

What I trying to do is Click on child(card) and do something with a parent(cell).

Is there a way how I could querySelector the parent e.g.

const selectedParentFromChild = document.querySelector(".child .parent")

Solution:
For those which were struggled as well. You can use parentElement like @Phix has mentioned down below in the comments.


Solution

  • try this:

    let child = document.querySelector(".child");
    let parent = child.parentNode;
    

    and you can't get the parent of a child via css only, there is currently no such selector see here