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

What does this mean in the script?

Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184
omg
  • 136,412
  • 142
  • 288
  • 348
  • 5
    @JMCF125 He managed to be useful anyway. I Googled for how to get the element that was clicked on in an onclick event, and ended up here, where I found the answer. – Throw Away Account Mar 31 '16 at 19:49
  • what does the javascript: do? why isnt it like this `here` – J3STER Jul 11 '17 at 23:33
  • 2
    @J3STER The "javascript:" prefix is incorrect in onclick. You can find the explanation in [Tim Down's answer below](https://stackoverflow.com/a/926170/146513). – Mariano Desanze Nov 10 '17 at 23:13

8 Answers8

114

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.
  • 108,298
  • 33
  • 122
  • 127
37

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">
</script>
<script type="text/javascript">
function func(e) {
  $(e).text('there');
}
</script>
<a onclick="func(this)">here</a>

(This example uses jQuery.)

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
Stephan202
  • 59,965
  • 13
  • 127
  • 133
24

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
  • 318,141
  • 75
  • 454
  • 536
  • 1
    Interesting downvote, although I suppose strictly speaking this answer only offers advice around the question rather than directly answering the question. – Tim Down Oct 21 '12 at 22:13
  • Yea... you didn't really answer the question :-/ nothing personal! – Dave Oct 23 '12 at 10:48
  • 1
    @Dave: Fair enough. By the time I wrote this the main question was already answered. My answer should probably have been a comment but I suspect I may not have had enough rep to add a comment at the time. Live and learn. – Tim Down Oct 23 '12 at 11:31
  • 9
    Not enough rep at the time? *spits out his wine* – Jonathan Nov 07 '14 at 18:17
10

In JavaScript this refers to the element containing the action. For example, if you have a function called hide():

function hide(element){
   element.style.display = 'none';
}

Calling hide with this will hide the element. It returns only the element clicked, even if it is similar to other elements in the DOM.

For example, you may have this clicking a number in the HTML below will only hide the bullet point clicked.

<ul>
  <li class="bullet" onclick="hide(this);">1</li>
  <li class="bullet" onclick="hide(this);">2</li>
  <li class="bullet" onclick="hide(this);">3</li>
  <li class="bullet" onclick="hide(this);">4</li>
</ul>
Satch3000
  • 47,356
  • 86
  • 216
  • 346
Steffan Perry
  • 2,112
  • 1
  • 21
  • 21
5

Here (this) is a object which contains all features/properties of the dom element. you can see by

console.log(this);

This will display all attributes properties of the dom element with hierarchy. You can manipulate the dom element by this.

Also describe on the below link:-

http://www.quirksmode.org/js/this.html

Deepak Dholiyan
  • 1,774
  • 1
  • 20
  • 35
5

keyword this in addEventListener event

function getValue(o) {
  alert(o.innerHTML);
}

function hide(current) {
  current.setAttribute("style", "display: none");
}

var bullet = document.querySelectorAll(".bullet");

for (var x in bullet) { 
  bullet[x].onclick = function() {
    hide(this);
  };
};
 
/* Using dynamic DOM Event */
document.querySelector("#li").addEventListener("click", function() {
  getValue(this); /* this = document.querySelector("#li") Object */
});
li {
  cursor: pointer;
}
<ul>
  <li onclick="getValue(this);">A</li>
  <li id="li" >B</li>
  <hr />
  <li class="bullet" >1</li>
  <li class="bullet" >2</li>
  <li class="bullet" >3</li>
  <li class="bullet" >4</li>
</ul>
antelove
  • 3,216
  • 26
  • 20
3

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
  • 643,351
  • 109
  • 780
  • 844
3

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
  • 6,162
  • 3
  • 27
  • 34