In my program, I have a click event that does the same thing for many buttons, except the class in a small part of the event handler function is different for each one. Right now, I'm just using an anonymous function for this. The function attached to this event is pretty long, and I anticipate there being many buttons ( maybe 60 or so? ). I could copy and paste the currently working function I have 60 times, but that will make my javascript file huge.
What I would like to do, is define the event handler function outside of the on() function, so I can just say $('#some_button').on('click', my_event_handler("my_arg") );
This will really simplify my code.
I made a js fiddle that kind of gives a very small example of what I'm trying to do:
http://jsfiddle.net/AsmmF/
The only problem, is that I can't get it to work!
I'm going to go see Ted, and try out your solutions to my problem when I get back.
Thanks much for any help that you can provide me!
You just need to wrap your function call in an anonymous function.
$('#some_button').on('click', function() {my_event_handler("my_arg");} );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With