Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with using .getAttribute() :( 2

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR
Can someone tell me how to get the tag of x?
I always get the "---- is not a function" error :(

After an extensive research it looks like you can get the attribute of something only if you have its Id.
Is that so?

The problem is that I want to get the tag type of something that doesn't have an Id.

Code:
function defocus( x ) {

    if( navigator.appName == 'Microsoft Internet Explorer' ) {

    self.focus();
    
    } else {

        if (x.getAttribute('tag') == 'a') { // <<< this doesn't work :(
        
        x.style.outline = null;
        
        } else {
        
        x.blur();
        
        }

    }

}


<a href="javascript: defocus(this);">click here</a>
 
Hi

Code:
[b]if[/b] [teal]([/teal]x[teal].[/teal]nodeName[teal].[/teal][COLOR=darkgoldenrod]lowerCase[/color][teal]()[/teal] [teal]==[/teal] [green][i]'a'[/i][/green][teal])[/teal] [teal]{[/teal] [gray]// then try this[/gray]

Feherke.
 
Thanks feherke :)

But there is an error saying :

Code:
Error: x.nodeName is undefined

Tested on Firefox 3
 

Thanks Dan, but "x.tagName" returns "undefined"

Feherke, as you can see in the code above, x refers to 'this', as shown in the html bit.

Code:
<a href="javascript: defocus(this);">click here</a>

 
Hi

Oops. Sorry. Missed that. However, my doubt was correct : x is not a reference to an element.

The [tt]this[/tt] keyword is a reference to the currently activated element if it is used in its event handler.
Code:
<a href="#" onclick="defocus(this);return false">click here</a>
Then the condition will work with [tt]x.getAttribute('tag')[/tt], [tt]x.nodeName.toLowerCase()[/tt] and [tt]x.tagName.toLowerCase()[/tt]. ( Sorry for the typo. )

Feherke.
 

Thanks a lot Feherke, I understand better now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top