By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,253 Members | 1,680 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,253 IT Pros & Developers. It's quick & easy.

getAttribute ('id') Not Working

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 #5

P: n/a
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') ;
Stephane : Adding the check for the tagName to be DIV made it work in
both IE and FF.
Martin : Checking for childNode type made it work in firefox but
somehow broke it in IE for some cases.

Thanks a ton .. Appreciate your quick help.

vivekian

Dec 5 '06 #6

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.