The Little Guy Posted September 30, 2008 Share Posted September 30, 2008 HTML: <a id="web" href="javascript:searchFor(this.id);">Web</a> JavaScript: function searchFor(s){ alert(s); } Quote Link to comment Share on other sites More sharing options...
xtopolis Posted October 1, 2008 Share Posted October 1, 2008 Info source: http://www.quirksmode.org/js/this.html Example is at the very end, but it's all worth the read to understanding -> "calling searchFor via href passes a call to the function, where the this keyword now refers to the window(browser), like this.searchFor instead of searchFor(this.element.id)... if that makes any sense "(read article for a better understand ==>"to get the correct this passed to your function you must register it to an onclick event" So I made it href="#" (or could be href="javascript: void(0);"), and added onclick="searchFor(this.id)" This works: <html> <head> <script type="text/javascript"> function searchFor(s){ alert(s); } </script> </head> <body> <a id="web" href="#" onclick="searchFor(this.id)">Web</a> </body> </html> Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 1, 2008 Author Share Posted October 1, 2008 Ahhh.. OK! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.