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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Difference in behaviour of the anchor tag 1

Status
Not open for further replies.

bean1234

Technical User
Nov 14, 2006
45
US
Hello All,

I want to call a javascript function on the click of a link ..if I use teh following I get an error saying that this.parentNode is not defined..

Code:
<a href="javascript:return toggle(this.parentNode.parentNode,this.firstChild,'button_collapse.gif','button_expand.gif');"><img src="button_collapse.gif" width="5" height="9" border="0" /> <b>External</b></a> </th></tr>

But if I use the following approah it works fine can anyone please point out whats that I am doing wrong.

Code:
<a href="#" onclick="javascript:toggle(this.parentNode.parentNode,this.firstChild,'button_collapse.gif','button_expand.gif');"><img src="button_collapse.gif" width="5" height="9" border="0" /> <b>External</b></a> </th></tr>

I want to use the former approach becuase I dont want to loose the focus when the link is clicked, when you use the later approach it somehow looses teh focus to the beginning of teh page and I am using IE..
 
See if this works:

Code:
<a href="javascript:void(toggle(this.parentNode.parentNode,this.firstChild,'button_collapse.gif','button_expand.gif'));">
 
You can still use the second solution (which is better) by retuning false at the end. This prevents the href from being triggered (which was then requesting the #).

Oh, lose the javascript: part of the onclick (not needed).
Code:
<a href="#" onclick="toggle(this.parentNode.parentNode,this.firstChild, 'button_collapse.gif', 'button_expand.gif'); [b]return false;[/b]">...</a>

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Thanks Jeff!

Dkdude I didnt try your approach as Jeff's solution works fine for now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top