Reputation: 329
I'm looking for a way to pass parameters to the event handler (addEventListener) when the click event of e. g. an button is triggered by code.
My solution is to add an own attribute to the element itself, like
document.getElementById("ButtonId").MyAttribute = MyValue ;
document.getElementById("ButtonId").click() ;
But I'm really interested in other solutions which should use first off all only JavaScript and then as an alternative JQuery. I'm aware of the JQuery event.data object, but maybe there are other solutions.
Many thanks and greetings
ANTLRStarter
Upvotes: 0
Views: 1399
Reputation: 3006
You may want to create the javascript function with parameter passing, and use addEventLister with your own parameter(s)
// javascript function with parameter passing
function myFunctionA(p1) {
// code
}
var v1 = document.getElementById("ButtonId");
v1.addEventListener("click", function(){myFunctionA("myParameter")}, false);
Upvotes: 1
Reputation: 356
The data object is the way to go. If you'd rather use pure JavaScript you could simply leverage HTML5's data-* attribute (http://www.w3schools.com/tags/att_global_data.asp).
Upvotes: 1