Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if my parentNode is a <a> html tag - YUI

Tags:

javascript

yui

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?

like image 592
JulienITARD Avatar asked Jun 11 '12 09:06

JulienITARD


People also ask

What is parentNode in HTML?

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.

How do I get parent nodes in HTML?

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.

How do I select parent nodes?

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.


1 Answers

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 !== "";
like image 96
Quentin Avatar answered Sep 23 '22 20:09

Quentin