tags:

views:

584

answers:

5
<a onclick="javascript:func(this)" >here</a>

what does this mean in the script?

+22  A: 

In the case you are asking about, this represents the HTML DOM element.

So it would be the <a> element that was clicked on.

TM
+1  A: 

this referes to the object the onclick method belongs to. So inside func this would be the DOM node of the a element and this.innerText would be here.

Gumbo
+1  A: 

It refers to the element in the DOM to which the onclick attribute belongs:

<script type="text/javascript"
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;
</script>
<script type="text/javascript">
function func(e) {
  $(e).text('there');
}
</script>
<a onclick="javascript:func(this)">here</a>

(This example uses jQuery.)

Stephan202
+1  A: 

When calling a function, the word "this" is a reference to the object that called the function.

In your example, it is a reference to the anchor element. At the other end, the function call then access member variables of the element through the parameter that was passed.

ichiban
A: 

The value of event handler attributes such as onclick should just be JavaScript, without any "javascript:" prefix. The javascript: pseudo-protocol is used in a URL, for example:

<a href="javascript:func(this)">here</a>

You should use the onclick="func(this)" form in preference to this though. Also note that in my example above using the javascript: pseudo-protocol "this" will refer to the window object rather than the <a> element.

Tim Down