tags:

views:

221

answers:

6

I was wondering which one of the above methods is preferred or is the general guideline that you use $(elem).click() for simple things and < a onclick="func(1, 2, 3, 4);" > when you need to pass parameters to the script when the element is clicked? I've been trying to only use $(elem).click() to keep the code clean and simple, but when you have to pass more than one parameter it gets tricky and function call on onclick="" seems like a better solution.

+5  A: 
$(elem).click(function() {
    func(1,2,3,4);
});

That seems like what you want, no?

Ryan Doherty
TheMagician
well then you should use the html5 data attributes to set unique values associated with each relevant element: `<a data-param="1">link</a>`
geowa4
If you have hundreds of these elements the most efficient way is using [event delegation][http://www.danwebb.net/2008/2/8/event-delegation-made-easy-in-jquery] instead of individual onclick handlers for each element. To keep the JS less mixed in with your HTML, you could store the needed parameters in a Javascript data structure, with keys for each object being the element ID or similar.
Ben
Technically it is faster to do onclick="foo()", this is more performant. But you shouldn't do that unless you are seeing horrible performance issues and have tried everything else.I like Ben's idea of event delegation + a JS array of information. Seems like a good idea.
Ryan Doherty
+2  A: 

I prefer to use $(elem).click() to keep my HTML javascript-free, I consider it cleaner.

marcgg
+5  A: 

One of the main ideas behind jQuery is unobtrusive javascript (i.e. HTML and javascript are two great tastes that don't taste great together). Using the jQuery object and selectors to attach the functions is what they would prefer you do.

Jason Punyon
+2  A: 

Hi,

If the parameters could be determined at the moment of the click, I would suggest the binding method:

$('a').click(function(){ someFunc(this.val, 2, 3, 4); })

One case I could think of of doing it inline, is if you're building multiple links in a loop where params 1, 2, 3 or 4 are varying according to a backend variable or something:

<% for( int i = 0; i < 4; i++) { %>
    <a onclick="someFunc(1,<%= i %>,3,4"></a>
<% } %>

Personally, I always try to bind to the event in a $(document).ready() callback.

Hope this helps

Mike Gleason jr Couturier
A: 

You can pass parameters to a function when you use jQuery:

$(".selector").click(function() { func(1,"2", someVar); });

I prefer to always use jQuery for my event handling. I don't like putting any behavior in my HTML, even with onclick and related events.

Also, if you programmatically click something using $(".selector").click() and you have a handler attached using both onclick and jQuery, it will call the jQuery handler first, then the handler set using the attribute. But if you click with your mouse, it's the other way around. That might be unexpected, and end up giving you headaches down the road.

geowa4
A: 

I'd go with the click() as well, if you have additional parameters that you want to pass to the function, add them to any of the object's attributes, I usually use rel like this:

<a href="#" class="link" rel="parameter1, parameter2, etc">Link</a>

in the script you can do this:

$('.link').click(function() {
 var parameters = $(this).attr('rel').split(',');
 // Do something
 return false;
})
fudgey