Search code examples
javascriptyui

How to check if my parentNode is a <a> html tag - 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?


Solution

  • 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 !== "";