Passing 'this' to an onclick event
The code that you have would work, but is executed from the global context, which means that this
refers to the global object.
<script type="text/javascript">
var foo = function(param) {
param.innerHTML = "Not a button";
};
</script>
<button onclick="foo(this)" id="bar">Button</button>
You can also use the non-inline alternative, which attached to and executed from the specific element context which allows you to access the element from this
.<script type="text/javascript">
document.getElementById('bar').onclick = function() {
this.innerHTML = "Not a button";
};
</script>
<button id="bar">Button</button>
What's the difference between onClick ={ () = function()} and onClick = {function()}?
In first one:
<button onClick={()=>props.submitHandler(searchInputValue)}>Submit</button>
This is arrow function and it will trigger only onClick of the button.In second one:
<button onClick={props.submitHandler(searchInputValue)}>Submit</button>
This is a normal function call , which calls the method as soon the rendering of the component happens. How does this in onClick work?
When you set an onclick attribute; the browser takes the reference to the node you've set it on, let's call this reference button
, and then does the transfomation
onclick="/* code */"
tobutton.onclick = function (event) {eval(/* code */);};
When a click event is then tirggered, the function can be thought of as being invoked like thisbutton.onclick.call(button, click_event);
Giving the onclick
the context of button
, and hence this
is button
. It is wrapped in it's own function so if something is var
d it will not be set in the global scope.ReturningWhat happens with a return value?
false
is the equivalent of calling event.preventDefault();
, exact behaviour is described here.Assumes JavaScript unless explicitly stated otherwiseHow does the browser know which script language to use?
Don't learn these HTML attributes, they are dated, can be dangerous and mix your HTML and JavaScript together. Instead learn to useWhat do I want to read to know what can I rely on when working with these attributes?
EventTarget.addEventListener
How do I find out what javascript function is being called by an object's onclick event?
You can do like this
With Javascript Demo on JsFiddle
div1 = document.getElementById('div1');
alert(div1.getAttribute("onclick"));
With jQuery Demo on JsFiddle<div id="div1" onclick="myfun();" >
alert($('#div1').attr('onclick'));
onClick to get the ID of the clicked button
You need to send the ID as the function parameters. Do it like this:
<button id="1" onClick="reply_click(this.id)">B1</button><button id="2" onClick="reply_click(this.id)">B2</button><button id="3" onClick="reply_click(this.id)">B3</button> <script type="text/javascript"> function reply_click(clicked_id) { alert(clicked_id); }</script>
What wrong with .onclick() event?
onClick()
is a function. The event handler is the argument to the function.
$w.onReady(() => {
$w('#button2').onClick(() => {
wixWindow.openLightbox("Conditions générales de vente");
});
});
Related Topics
Node.Js Piping the Same Readable Stream into Multiple (Writable) Targets
Bootstrap: Open Another Modal in Modal
How to Validate Google Recaptcha V2 Using JavaScript/Jquery
Access Properties of the Parent with a Handlebars 'Each' Loop
Can Jquery Provide the Tag Name
Basic Authentication with Fetch
How to Reload/Refresh Jquery Datatable
JavaScript Array Concat Not Working. Why
Find the Exact Height and Width of the Viewport in a Cross-Browser Way (No Prototype/Jquery)
Check If Array Contains All Elements of Another Array
Read a Text File Using Node.Js
How to Sort an Associative Array by Its Values in JavaScript
JavaScript - Telling Setinterval to Only Fire X Amount of Times
Get the Current Year in JavaScript
Check If Year Is Leap Year in JavaScript