473,395 Members | 1,742 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,395 software developers and data experts.

getAttribute ('id') Not Working

Hi ,

In one of the javascript functions , i need to get the ids of the
elements. This function works in Internet Explorer :

var TabNames = ParentTab.childNodes ;

for ( i = 0 ; i < TabNames.length ; i++ )
{
var TabID = TabNames[i].getAttribute('id') ;
var TabIDSuffix = TabID.split (Splitter) ;

if ( TabIDSuffix[0].length != 0 )
{
if ( TabIDSuffix[0] == URLTab )
{
result = true ;
break ;
}
}

}
// TabNames is the array of divs which are enclosed in the parent div.

But in Firefox it gives an error saying :
Error: TabNames[i].getAttribute is not a function

Is there an alternate way to do this.

Dec 5 '06 #1
6 21461
VK

vivekian wrote:
In one of the javascript functions , i need to get the ids of the
elements. This function works in Internet Explorer :

var TabNames = ParentTab.childNodes ;

for ( i = 0 ; i < TabNames.length ; i++ )
{
var TabID = TabNames[i].getAttribute('id') ;
var TabIDSuffix = TabID.split (Splitter) ;

if ( TabIDSuffix[0].length != 0 )
{
if ( TabIDSuffix[0] == URLTab )
{
result = true ;
break ;
}
}

}
// TabNames is the array of divs which are enclosed in the parent div.

But in Firefox it gives an error saying :
Error: TabNames[i].getAttribute is not a function
You are hitting the phantom nodes problem on Gecko-based browsers
<http://en.wikipedia.org/wiki/Phantom_nodes>

You have to add extra check to sort out element nodes and parasite text
nodes from your pretty-print. See for instance
<http://developer.mozilla.org/en/docs/firstChild(skip on pathetic
arguments why phantom nodes are not a bug and see the samples
themselves).

You can also use anti-phantom pretty-print:

<body
><h1>Header</h1
><p>Content</p
<p>More content
....

but unless you have an automation script (mine is on Perl) that's a
boring doing.

Dec 5 '06 #2
vivekian wrote:
In one of the javascript functions , i need to get the ids of the
elements. This function works in Internet Explorer :

var TabNames = ParentTab.childNodes ;

for ( i = 0 ; i < TabNames.length ; i++ )
There could be a text node or comment node as a child which does not
have attribute so do a check for element nodes first e.g.
{
var childNode = TabNames[i];
if (childNode.nodeType === 1) {
// now you have an element and can access attributes of the element
var TabID = TabNames[i].getAttribute('id') ;
}

If that is HTML that you script then simply use
childNode.id
not
childNode.getAttribute('id')
--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 5 '06 #3
vivekian wrote:
Hi ,

In one of the javascript functions , i need to get the ids of the
elements. This function works in Internet Explorer :

var TabNames = ParentTab.childNodes ;

for ( i = 0 ; i < TabNames.length ; i++ )
{
var TabID = TabNames[i].getAttribute('id') ;
var TabIDSuffix = TabID.split (Splitter) ;

if ( TabIDSuffix[0].length != 0 )
{
if ( TabIDSuffix[0] == URLTab )
{
result = true ;
break ;
}
}

}
// TabNames is the array of divs which are enclosed in the parent div.

But in Firefox it gives an error saying :
Error: TabNames[i].getAttribute is not a function

Is there an alternate way to do this.
Does TabNames[i].id return something usable?

Erwin Moller
Dec 5 '06 #4
In article <11*********************@16g2000cwy.googlegroups.c om>,
vivekian <vi********@gmail.comwrote:
>In one of the javascript functions , i need to get the ids of the
elements. This function works in Internet Explorer :

var TabNames = ParentTab.childNodes ;

for ( i = 0 ; i < TabNames.length ; i++ )
{
var TabID = TabNames[i].getAttribute('id') ;
var TabIDSuffix = TabID.split (Splitter) ;

if ( TabIDSuffix[0].length != 0 )
{
if ( TabIDSuffix[0] == URLTab )
{
result = true ;
break ;
}
}

}
// TabNames is the array of divs which are enclosed in the parent div.

But in Firefox it gives an error saying :
Error: TabNames[i].getAttribute is not a function
You are almost certainly hitting the text nodes as well as the required
nodes - check with the DOM Inspector - you should probably check the
nodeType or use hasAttributes() before calling getAttributes.

--
John P Baker
Dec 5 '06 #5
ASM
vivekian a écrit :
In one of the javascript functions , i need to get the ids of the
elements. This function works in Internet Explorer :
Are your elements really have an id (and not only a name) ?
IE love too much to call a name an id and vice versa.
But in Firefox it gives an error saying :
Error: TabNames[i].getAttribute is not a function

Is there an alternate way to do this.
for ( i = 0 ; i < TabNames.length ; i++ )
{
if(TabNames[i] && TabNames[i].tagName=='DIV' && TabNames[i].id)
var TabID = TabNames[i].getAttribute('id') ;

Or try :

for ( i = 0 ; i < TabNames.length ; i++ )
{
if(TabNames[i] && TabNames[i].id)
var TabID = TabNames[i].id;

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Dec 5 '06 #6

vivekian wrote:
Hi ,

In one of the javascript functions , i need to get the ids of the
elements. This function works in Internet Explorer :

var TabNames = ParentTab.childNodes ;
Others have given you some ideas, but always room for more: :-)

var TabNames = ParentTab.getElementsByTagName('div');

Now TabNames contains *only* div elements, not all the childNodes of
TabNames. Or you could use feature detection (see below).

>
for ( i = 0 ; i < TabNames.length ; i++ )
{
var TabID = TabNames[i].getAttribute('id') ;
var TabIDSuffix = TabID.split (Splitter) ;
How about testing for the id property rather than using getAttribute:

var TabID, TablDSuffix;
if ((TabID = TabNames[i].id) && (TabIDSuffix =
TabID.split (Splitter))){
/* Do stuff with TabIDSuffix */
}
--
Rob

Dec 5 '06 #7

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

Similar topics

1
by: David | last post by:
Hi, I'm trying to parse an xml file and am a bit confused. I have created my class XmlParser. Also I have 3 other questions. 1-How to the GetAttribute to search for the value of a specific...
2
by: CES | last post by:
All, I'm at a loss, the code below works in IE but not in Netscape and I'm unskilled enough not to know why. Essentially this code looks thru all of the form fields and if the input box has a...
3
by: F. Da Costa | last post by:
Hi, I was wondering *why* there is a difference between the results of the following two statements. On the suface they seem to do the same (or do they?) frm => returns void ...
1
by: mr_burns | last post by:
Hi, I have the following code that works fine except the getAttribute line: var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async="false"; xmlDoc.load("note.xml"); nodes =...
6
by: Banski | last post by:
Hi, Im quite new to XML in .Net. Im getting values from an xml file using XPath and sorting as you see in the code below. I cant figure out how to get the value of date with GetAttribute. Hope...
2
by: CES | last post by:
All, I'm 2 for 2 on stupid questions this works in FireFox but not in IE it's not returning the value of the style element and I'm getting error?? document.getElementById('test').innerHTML =...
13
by: Noa | last post by:
Hi I have a page that looks like that: <form name="kuku1" action ="anotherpage.html" > <input name="name"> <input name="kuku2"> </form> As far as i know, "getAttribute" should return a...
6
by: vivekian | last post by:
Hi , In one of the javascript functions , i need to get the ids of the elements. This function works in Internet Explorer : var TabNames = ParentTab.childNodes ; for ( i = 0 ; i <...
4
by: ICPooreMan | last post by:
I've got some code which works in firefox that's giving me fits in IE7 (maybe other versions too I haven't tested it). What I want to do is get the oncontextmenu attribute of something, change the...
4
by: RobG | last post by:
I have always accessed attributes such as disabled using the DOM element property, however I was wondering about implementing a more generic function to get the values of attributes - which of...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.