473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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[i].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

Mar 28 '07 #1
2 1183
Ivo
<hg*****@bcbsal.orgschreef
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[i].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[i].firstChild.tagName.toLowerCase() != 'img' )

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

if( a[i].firstChild.src )

Alternatively, check a[i].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/
Mar 28 '07 #2
On Mar 28, 11:02 am, "Ivo" <n...@thank.youwrote:
<hgra...@bcbsal.orgschreef
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[i].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[i].firstChild.tagName.toLowerCase() != 'img' )

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

if( a[i].firstChild.src )

Alternatively, check a[i].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!

Mar 28 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
28
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
7
by: Jonathan Fine | last post by:
Giudo has suggested adding optional static typing to Python. (I hope suggested is the correct word.) http://www.artima.com/weblogs/viewpost.jsp?thread=85551 An example of the syntax he proposes...
24
by: Xah Lee | last post by:
in computer languages, often a function definition looks like this: subroutine f (x1, x2, ...) { variables ... do this or that } in advanced languages such as LISP family, it is not uncommon...
56
by: Xah Lee | last post by:
What are OOP's Jargons and Complexities Xah Lee, 20050128 The Rise of Classes, Methods, Objects In computer languages, often a function definition looks like this: subroutine f (x1, x2, ...)...
5
by: Xiangliang Meng | last post by:
Hi, all. What are the benefit and the drawback of defining a class embedded inside another class? For example: class List { public:
24
by: Romeo Colacitti | last post by:
Hi, Does anyone here have a strong understanding for the meanings of the terms "lvalue" and "rvalue" as it pertains to C, objects, and different contexts? If so please share. I've been...
15
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int...
18
by: Xah Lee | last post by:
What are OOP's Jargons and Complexities Xah Lee, 20050128 Classes, Methods, Objects In computer languages, often a function definition looks like this: subroutine f (x1, x2, ...) {...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.