I want to wrap an existing click event in some additional code.
Basically I have a form for several parts in the accordion, and I want to activate the check by clicking on the header of the accordion. The accordion code is used elsewhere and I do not want to change it.
Here is what I tried:
//Take the click events off the accordion elements and wrap them to trigger validation $('.accordion h1').each(function (index, value) { var currentAccordion = $(value); //Get reference to original click var originalClick = currentAccordion.click; //unbind original click currentAccordion.unbind('click'); //bind new event currentAccordion.click(function () { //Trigger validation if ($('#aspnetForm').valid()) { current = parseInt($(this).next().find('.calculate-step').attr('data-step')); //Call original click. originalClick(); } }); });
jQuery throws an error as it tries to execute this.trigger inside the originalClick function, and I don't think this is what JQuery expects.
EDIT: Updated code. It works, but it's a little ugly!
//Take the click events off the accordion elements and wrap them to trigger validation $('.accordion h1').each(function (index, value) { var currentAccordion = $(value); var originalClick = currentAccordion.data("events")['click'][0].handler; currentAccordion.unbind('click'); currentAccordion.click(function (e) { if ($('#aspnetForm').valid()) { current = parseInt($(this).next().find('.calculate-step').attr('data-step')); $.proxy(originalClick, currentAccordion)(e); } }); });
source share