0

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

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

2 Comments

That's really an interesting approach. Haven't been aware of that HTML5 feature. Many thanks for that hint.
@ANTLRStarter you're welcome. If you find it ended up being the best answer please mark it as such, thanks :)
1

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);

1 Comment

Yep, but how/where can I assign the value of myParameter?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.