Jump to content

[SOLVED] why am I getting undefined?


The Little Guy

Recommended Posts

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.