Select the parent node of an element with CSSS [duplicated]

0

The code that I have been given has elements that are dynamically painted, are informative and are within a , I would like to know how I can, having the id of a div that is within this a , select by CSS that a .

<a href="x">
     <div id="z">
     </div>
</a>

So, having the div #z , how do I select the node that encapsulates this div using css?

    
asked by Andress Blend 18.05.2017 в 18:31
source

2 answers

1

Only through CSS can not yet, supposedly there are requests for the update to the specification include power to reference "backwards", but as is now can not. Understanding from a child back to know who is the father (a genealogical fatality, but good ....)

If you use jQuery you can position yourself in an element and know who your father is. For this you should use .parent () , .parents () , or .parentsUntil ()

You could also use : parent , but as indicated in the documentation, it is part of jQuery and not CSS.

Without jQuery you can use the parentElement property of the DOM object (ref. link )

    
answered by 18.05.2017 / 20:23
source
0

Here is an example of how to access the element <a> from <div> through id and also an example of how to access the element <a> through a class .

Example:

// a traves del id:
el = document.getElementById("z").parentElement;

console.log(el);

// a traves de una clase:
el = document.getElementsByClassName("z");

console.log(el);
console.log(el.length);

for(i = 0; i <el.length; i++) {
 console.log(el[i]);
 console.log(el.item(i));
 console.log(el.namedItem(i));
}
<a href="x" class="z">
     <div id="z">
     </div>
</a>
    
answered by 18.05.2017 в 19:20