I have : <a href="mylink.html"><img src="abc.png" /></a>
.
I want to know if my img
parentNode is a <a>
tag or not.
I use YUI 3 library, but I can use native JS.
Any suggestions?
The read-only parentNode property of the Node interface returns the parent of the specified node in the DOM tree. Document and DocumentFragment nodes can never have a parent, so parentNode will always return null . It also returns null if the node has just been created and is not yet attached to the tree.
To get the parent node of an HTML element, you can use the parentNode property. This property returns the parent node of the specified element as a Node object.
Approach: Write a recursive function that takes the current node and its parent as the arguments (root node is passed with -1 as its parent). If the current node is equal to the required node then print its parent and return else call the function recursively for its children and the current node as the parent.
Grab the parent node, get its node name, then compare to the node name you want. To make sure something is a link (rather than some other kind of anchor), test to see if it has a filled in href property.
node.parentNode.nodeName.toLowerCase() === 'a' && node.parentNode.href !== "";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With