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); } Link to comment https://forums.phpfreaks.com/topic/126533-solved-why-am-i-getting-undefined/ 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> Link to comment https://forums.phpfreaks.com/topic/126533-solved-why-am-i-getting-undefined/#findComment-654374 Share on other sites More sharing options...
The Little Guy Posted October 1, 2008 Author Share Posted October 1, 2008 Ahhh.. OK! Link to comment https://forums.phpfreaks.com/topic/126533-solved-why-am-i-getting-undefined/#findComment-654380 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.