. I want to know if my imgparentNode i...">

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

I: <a href="mylink.html"><img src="abc.png" /></a>. I want to know if my imgparentNode is a tag <a>or not.

I use the YUI 3 library, but I can use my own JS.

Any suggestions?

+5
source share
3 answers

Take the parent node, get its name node, then compare with the name of the node you want. To make sure something is a link (and not some other anchor), check to see if it has a filled href property.

node.parentNode.nodeName.toLowerCase() === 'a' && node.parentNode.href !== "";
+8
source

If you use YUI, then imgNode.ancestor().test('a').

+2
source

You are using node.parentNode. A simple search would show it.

+1
source

All Articles