I have a radio button, containing an inline onclick event:
<input type="radio" class="quantitycheckbox" onclick="alterQuantity(this.form)">
I’m trying to unobtrusively add some more functionality on click, and have this code:
function updateCustomFields( box ) {
// save current onclick event before assigning new one
var currEvent = box.onclick;
box.onclick = function() {
currEvent();
// do additional stuff here
}
}
var boxes = $class('quantitycheckbox');
for( var i = 0; i < boxes.length; i++ ) {
updateCustomFields( boxes[i] );
}
However, the value of this.form that’s passed into the alterQuantity function is undefined, and I get an error. How would I preserve the onclick event as it is and add my functionality?
You don’t need to do anything with the inline onclick event. Simply by attaching another event using
attachEventoraddEventListenerwill solve your problem since the inlineonclickevent will always fire first:The execution order will be: