Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
120
rated 0 times [  127] [ 7]  / answers: 1 / hits: 30977  / 14 Years ago, sat, july 10, 2010, 12:00:00

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

}

More From » jquery

 Answers
15

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




[#96279] Thursday, July 8, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kylanalis

Total Points: 438
Total Questions: 85
Total Answers: 102

Location: Barbados
Member since Sun, Nov 27, 2022
1 Year ago
kylanalis questions
Sat, Oct 2, 21, 00:00, 3 Years ago
Tue, Oct 13, 20, 00:00, 4 Years ago
Thu, Feb 13, 20, 00:00, 4 Years ago
Tue, Jan 7, 20, 00:00, 4 Years ago
;