views:

65

answers:

4

Is it possible to add a onclick event to any button by jquery or something like we add class?

function onload()
{

//add a something() function to button by id

}
+2  A: 
$('#id').click(function() {
    // do stuff
});
melhosseiny
+1 for same JS comment ;)
Jason McCreary
+3  A: 

Yes. You could write it like this:

$(document).ready(function() {
  $(".button").click(function(){
    // do something when clicked
  });
});
Ken Earley
+2  A: 

Yes. Something like the following should work.

$('#button_id').click(function() {
  // do stuff
});
Jason McCreary
+1  A: 

Calling your function something binding the click event on the element with a ID

$('#id').click(function(e) {
    something();
});

$('#id').click(something);

$('#id').bind("click", function(e) { something(); });

Live has a slightly difference, it will bind the event for any elements added, but since you are using the ID it probably wont happen, unless you remove the element from the DOM and add back later on (with the same ID).

$('#id').live("click", function(e) { something(); });

Not sure if this one works in any case, it adds the attribute onclick on your element: (I never use it)

$('#id').attr("onclick", "something()");

Documentation

BrunoLM
some comment would've been neat for these lines. He may not get what they are good for.
galambalazs