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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.