Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
133
rated 0 times [  139] [ 6]  / answers: 1 / hits: 56738  / 13 Years ago, thu, march 3, 2011, 12:00:00

I have a click event that is defined already. I was wondering what the best way would be to append another event handler to this event without touching this code (if possible).



Is there a way to just append another event handler to the already defined click event?



This is the current click event definition:



 $(.HwYesButton, #HwQuizQuestions)
.append(<a href='javascript:void(0);' rel='.HwYesButton'>Yes</a>)
.click(function(event) {
var button = $(this).parent();
var questionId = button.attr('id');
$iadp.decisionPoint[HwQuizYourself].Input.inputProvided(questionId);
button.find(.HwNoButton).removeClass(HwButtonSelected);
$(this).addClass(HwButtonSelected);
button.removeClass(HwQuestionSkipped);

$iadp.flat.setBoolean(questionId, true);
return false;
});

More From » jquery

 Answers
10

The only thing you can do is to attach another (additional) handler:



$(.HwYesButton, #HwQuizQuestions).click(function() {
// something else
});


jQuery will call the handlers in the order in which they have been attached to the element.



You cannot extend the already defined handler.






Btw. your formulation is a bit imprecise. You are not defining a click event. You are only attaching click event handlers. The click event is generated by the browser when the user clicks on some element.



You can have as many click handlers as you want for one element. Maybe you are used to this in plain JavaScript:



element.onclick = function() {}


With this method you can indeed only attach one handler. But JavaScript provides some advanced event handling methods which I assume jQuery makes use of.


[#93460] Wednesday, March 2, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mireyag

Total Points: 73
Total Questions: 107
Total Answers: 85

Location: Ukraine
Member since Sun, Dec 13, 2020
3 Years ago
mireyag questions
Sun, Aug 15, 21, 00:00, 3 Years ago
Wed, Dec 16, 20, 00:00, 3 Years ago
Tue, Sep 1, 20, 00:00, 4 Years ago
Sun, Jul 5, 20, 00:00, 4 Years ago
;