In languages like Ruby or Java, this (or in case of Ruby self) will always point to the object in which your method is defined. So in Ruby if you are working on the foo method inside the Bar class, self will always point to the object which is the instance of the Bar class.

JavaScript works quite surprisingly here. Because in JavaScript function context is defined while calling the function, not while defining it! This is what can surprise many when coming to JS from different fields. Such late binding is a powerful mechanism which allows us to re-use loosely coupled functions in variety of contexts.

To answer your question, this, when called from an onclick event handler is executed by the global context.

Answer from AndreasLengkeek on Stack Overflow
🌐
W3Schools
w3schools.com › jsref › event_onclick.asp
onclick Event
<h3 onclick="myFunction(this, 'red')">Click me to change my color.</h3> <script> function myFunction(element, color) { element.style.color = color; } </script> Try it Yourself » · Click to copy text from one input field to another: <button onclick="myFunction()">Copy Text</button> function myFunction() { document.getElementById("field2").value = document.getElementById("field1").value; } </script> Try it Yourself » ·
🌐
W3Schools
w3schools.com › tags › ev_onclick.asp
HTML onclick Event Attribute
<button onclick="myFunction()">Copy Text</button> <script> function myFunction() { document.getElementById("field2").value = document.getElementById("field1").value; } </script> Try it Yourself » · HTML DOM reference: onclick event ❮ HTML Event Attributes ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected] · If you want to report an error, or if you want to make a suggestion, send us an e-mail: [email protected] · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
Kodeclik
kodeclik.com › how-to-get-value-of-button-clicked-in-javascript
Getting the value of a clicked button in Javascript
July 20, 2025 - <body> <h2>Kodeclik Button Tutorial</h2> <button id="1" name="Button1" value="Humpty" onclick="f(this.value)"> Humpty </button> <button id="2" name="Button2" value="Dumpty" onclick="f(this.value)"> Dumpty </button> <button id="3" name="Button3" value="Sat on a Wall" onclick="f(this.value)"> Sat on a Wall </button> <H2 id="message"> </H2> <script> function f(val) { document.getElementById("message").innerHTML = val; } </script> </body> The functionality of the page is the same as before. When the third button is clicked, we get: You have learnt how to get the value of a button clicked in Javascript.
🌐
How To Code School
howtocodeschool.com › 2021 › 04 › get-input-element-value-on-click-using-javascript.html
Get input Element Value On Click Using JavaScript
The HTML Dom getElementsByTagName method returns the collection of element with the specified tag name. In this code it is used to get the input element. After targeting the input element, .value method is used to get it's value.
🌐
WebDeveloper.com
webdeveloper.com › community › 236420-how-do-i-pass-a-value-onclick-with-jquery
How do I pass a value onclick with jquery?
Copy linkTweet thisAlerts: @NihilisteOct 01.2010 — #For example you could do something like this: [CODE]<script type="text/javascript"> $("#number_filters a").bind("click", function() { var value = $( this ).attr( 'href' ); return false; }); </script> [/CODE] In this case the $( this ) selector will refer to the <a> that was clicked.
Find elsewhere
🌐
NamePros
namepros.com › forums › marketplace › external sales and everything else › free stuff › source code
Very cool Javascript function - alternative to onclick="this.value=''" | NamePros
February 19, 2005 - <input type="text" name="email" value="Enter your email address" onclick="this.value=''" /> and then when you click off and back on to edit something, it resets it again, you can use this function; Code: <script language="Javascript"> <!-- function resetBox(box, defaultvalue) { if (box.value == defaultvalue) { box.value = ""; } } --> </script> This way, you can use a textbox like this; Code: <input type="text" name="email" value="enter your email" onfocus="resetBox(this, 'enter your email')" /> The views expressed on this page by users and staff are their own, not those of NamePros.
🌐
Sololearn
sololearn.com › en › Discuss › 2798188 › onclick-should-run-a-function-and-when-clicked-again-should-returndisplay-the-original-value
Onclick should run a function and when clicked again should return/display the original value | Sololearn: Learn to code for FREE!
When I clicked on a button I want it to run a function and when the buttons is clicked again I want it to go back to his default value. E.g. When I clicked on the button it should display "added to cart" when clicked again it should display "remove from cart" and the cycle continue. ///////////////The JavaScript code////// counter = 0; var number0fItems = document.getElementById("counter"); number0fItems.innerHTML = counter; /*this loops through the button to know which button was clicked*/ document.querySelectorAll(".eventBtn").forEach(function(item){ /*the var firstime is to stop the counter from iterating multiple times when the button is clicked multiple times*/ var firsttime = true; item.onclick = function(event){ if (firsttime){ firsttime = false; this.innerHTML = "Remove from cart"; this.style.backgroundColor = "#FFE9D6"; counter++; number0fItems.innerHTML = counter; } } })
🌐
Quora
quora.com › How-do-you-pass-a-button-value-to-a-JavaScript-function
How to pass a button value to a JavaScript function - Quora
Answer (1 of 3): if you are using any global event handlers like onclick, ondblclick inside the tag you can add the this keyword as a argument and same goes for the actual function declaration.
🌐
freeCodeCamp
freecodecamp.org › news › html-button-onclick-javascript-click-event-tutorial
HTML Button onclick – JavaScript Click Event Tutorial
August 16, 2021 - This time around, the onclick functions in our HTML take the values of the color we want to change the text to. These are called parameters in JavaScript.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › this
this - JavaScript | MDN
Think about this as a hidden parameter ... For a regular function (not an arrow function, bound function, etc.), the value of this is the object that the function is accessed on....