How to pass event as argument to an inline event handler in JavaScript?
to pass the event
object:
<p id="p" onclick="doSomething(event)">
to get the clicked child element
(should be used with event
parameter:
function doSomething(e) {
e = e || window.event;
var target = e.target || e.srcElement;
console.log(target);
}
to pass the element
itself (DOMElement):
<p id="p" onclick="doThing(this)">
see live example on jsFiddle.
You can specify the name of the event
as above, but alternatively your handler can access the event
parameter as described here: "When the event handler is specified as an HTML attribute, the specified code is wrapped into a function with the following parameters". There's much more additional documentation at the link.
How to pass an event object to a function in Javascript?
Although this is the accepted answer, toto_tico's answer below is better :)
Try making the onclick js use 'return' to ensure the desired return value gets used...
<button type="button" value="click me" onclick="return check_me();" />
What all information we can pass as parameters to inline event handlers?
I believe you can refer to any property that's somewhere in the element's prototype chain as a standalone variable:
function someFunction(id, onclick, children, clientTop) { console.log(id); // someId console.log(onclick); // note, this is null, not undefined! console.log(children); // length 0, but still an HTMLCollection console.log(clientTop); // 2}
<input type="text" value="Hello" onchange="someFunction(id, onclick, children, clientTop);" id="someId" />
Passing event data in the inline event handlers
<input onkeydown="myfunc(event)">
function myfunc (e) {
e = e || window.event;
var code = e.keyCode;
alert (code);
}
How to pass multiple arguments in an inline event listener?
to get to the underlying event you can use $event
see https://v3.vuejs.org/guide/events.html#methods-in-inline-handlers
React onClick - pass event with parameter
Try this:
<button
onClick={(e) => {
this.clickMe(e, someParameter);
}}
>
Click Me!
</button>
And in your function:
function clickMe(event, someParameter){
//do with event
}
How do you pass the target element as an argument to an inline event handler?
Looks fine to me
<script type="text/javascript">
function someEvent(t) {
alert(t.tagName)
}
</script>
<a onclick='someEvent(this)'>Click me</a>
I think the cause of the problem is dynamic element generation. How are you adding the attribute to the element?
Why not just use addEventHandler
during the dynamic element generation?
passing extra parameter to event handler
You can use the .bind method and the arguments object inside the function to access extra parameters you want to pass in
$(function(){
$('#btn').click(function(){
var id = $(this).attr('id');
util.doSomething({
property1: "SomeValue1",
property2: "SomeValue2",
onDone: update.bind(this, id)
})
})
function update()
{
console.log(arguments); // inside arguments you should see all your parameters
// arguments[0] your id
// arguments[1] your response
}
})
Related Topics
How to Remove Time from Datetime in SQL
Label and Input Fields on the Same Line
How to Get the Difference Between Two Arrays of Objects in JavaScript
Auto Populate Field Base on What Was Entered in Another Field Simultaneously
Uncaught (In Promise) Typeerror: Failed to Fetch and Cors Error
Referenceerror: Cannot Access 'Player' Before Initialization
Expand Div on Click With Smooth Animation
How to Strip/Replace Something from a Url
How to Close Bootstrap 3 Dropdown When We Click Outside on a Tablet
Javascript, Viewing [Object Htmlinputelement]
How to Remove the X-Axis from a Bar Chart Produced by Google'S Visualization API
Formatting a Number to Have Commas At Every 1000 Factor
How to Only Remove the "Watch Later" and "Share" Buttons from Youtube Iframe Embed Player
Convert Jquery Code to JavaScript