Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not Using an Anonymous Function w/ Jquery Events

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!

like image 629
Camden Reslink Avatar asked Dec 27 '22 22:12

Camden Reslink


1 Answers

You just need to wrap your function call in an anonymous function.

$('#some_button').on('click', function() {my_event_handler("my_arg");} );

like image 73
dcpomero Avatar answered Jan 12 '23 04:01

dcpomero