What is the syntax to check whether a tag is inside another tag?

H

hgraham

Hi,

I've searched for this everywhere and can't seem to find the answer.
I'm sure this is something really simple. I have an if statement and
I need to check to see whether there is an img tag within an anchor
tag. I tried the following:

var e = document.getElementById('contentWrapper');
if (e)
{
var a=e.getElementsByTagName('a');
for (var i=0;i<a.length;i++)
{
if (a.firstChild != 'img')) <-- How do I check to see if the
anchor is an img?
{
blah, blah, blah....

Can anyone tell me what the syntax should be for the statement in
question?

Thanks!!!
Holli
 
I

Ivo

I've searched for this everywhere and can't seem to find the answer.
I'm sure this is something really simple. I have an if statement and
I need to check to see whether there is an img tag within an anchor
tag. I tried the following:
if (a.firstChild != 'img')) <-- How do I check to see if the
anchor is an img?


By properly balancing the brackets, and checking the firstChild's tagName
property instead of the object itself. Browsers vary in their reports of
tagnames, which is overcome by the toLowerCase bit.

if( a.firstChild.tagName.toLowerCase() != 'img' )

Or, because only images and really weird objects have a src attribute, this
may be slightly faster:

if( a.firstChild.src )

Alternatively, check a.getElemenetsByTagName('img').length for its value.
This will also return images that are the anchor's second or third child.
Some browsers line up empty textnodes (such as a newline in your code) as
children, while others (notably IE) don't, so even if the image is the first
item in the anchor, depending on coding style, FF cs may not report it as a
firstChild.
hth
ivo
http://4umi.com/web/javascript/
 
H

hgraham

I've searched for this everywhere and can't seem to find the answer.
I'm sure this is something really simple. I have an if statement and
I need to check to see whether there is an img tag within an anchor
tag. I tried the following:
if (a.firstChild != 'img')) <-- How do I check to see if the
anchor is an img?


By properly balancing the brackets, and checking the firstChild's tagName
property instead of the object itself. Browsers vary in their reports of
tagnames, which is overcome by the toLowerCase bit.

if( a.firstChild.tagName.toLowerCase() != 'img' )

Or, because only images and really weird objects have a src attribute, this
may be slightly faster:

if( a.firstChild.src )

Alternatively, check a.getElemenetsByTagName('img').length for its value.
This will also return images that are the anchor's second or third child.
Some browsers line up empty textnodes (such as a newline in your code) as
children, while others (notably IE) don't, so even if the image is the first
item in the anchor, depending on coding style, FF cs may not report it as a
firstChild.
hth
ivohttp://4umi.com/web/javascript/


I checked for img.length like you suggested and it works great!
Thanks for the help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top