Connecting Tech Pros Worldwide Help | Site Map

getAttribute ('id') Not Working

  #1  
Old December 5th, 2006, 02:35 PM
vivekian
Guest
 
Posts: 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.

  #2  
Old December 5th, 2006, 02:55 PM
VK
Guest
 
Posts: n/a

re: getAttribute ('id') Not Working



vivekian wrote:
Quote:
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
Quote:
><h1>Header</h1
Quote:
><p>Content</p
><p>More content
....

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

  #3  
Old December 5th, 2006, 02:55 PM
Martin Honnen
Guest
 
Posts: n/a

re: getAttribute ('id') Not Working


vivekian wrote:
Quote:
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.
Quote:
{
var childNode = TabNames[i];
if (childNode.nodeType === 1) {
// now you have an element and can access attributes of the element
Quote:
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/
  #4  
Old December 5th, 2006, 03:05 PM
Erwin Moller
Guest
 
Posts: n/a

re: getAttribute ('id') Not Working


vivekian wrote:
Quote:
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
  #5  
Old December 5th, 2006, 03:05 PM
JP. Baker
Guest
 
Posts: n/a

re: getAttribute ('id') Not Working


In article <1165330667.536168.85350@16g2000cwy.googlegroups.c om>,
vivekian <viveklinux@gmail.comwrote:
Quote:
>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
  #6  
Old December 5th, 2006, 03:05 PM
ASM
Guest
 
Posts: n/a

re: getAttribute ('id') Not Working


vivekian a écrit :
Quote:
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.
Quote:
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
  #7  
Old December 5th, 2006, 08:35 PM
RobG
Guest
 
Posts: n/a

re: getAttribute ('id') Not Working



vivekian wrote:
Quote:
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).

Quote:
>
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

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
IE code is Not compatible with firefox shyamg answers 8 June 20th, 2007 11:52 AM
getAttribute ('id') Not Working vivekian answers 6 December 5th, 2006 08:35 PM
getAttribute('style') not working in IE CES answers 2 November 24th, 2005 11:15 PM
Alert not working in IE6 some cases schieco answers 2 July 23rd, 2005 11:25 AM