I’m having trouble trying to properly insert additional code into the onclick attribute of a submit button. I don’t want to overwrite anything within the onclick but just add additional code. I have successfully inserted the code needed but when I look at the source code of the button it shows some additional functionality that shouldn’t be there so i don’t think I have it written out properly.
Here’s the button:
<input id='TESTFrmSubmit' type='submit' value='Submit' name='submitButton' onclick='formSubmit(document.getElementById("TESTForm_1001")); return false;' />
The additional code I want to insert into the onclick attribute is:
'_gaq.push(['_trackEvent', 'Whitepaper Campaign', 'Submit Button','conceptshareexample.pdf'']);'
The jquery I have that inserts the additonal code into the onclick attribute is:
$(document).ready(function(){
$("#TESTFrmSubmit").attr ("onclick", function() {
return this.onclick + "'_gaq.push(['_trackEvent', 'Whitepaper Campaign', 'Submit Button','TESTexample.pdf'']);'";
});
});
The problem I’m having is that when i view the source code of the button, it contains
function onclick(event) within the onclick displaying it like so:
onclick="function onclick(event){ formSubmit(document.getElementById("TESTForm_1010")); return false; }_gaq.push(['_trackEvent', 'Whitepaper Campaign', 'Submit Button','TESTexample.pdf'']);"
I need it to display it like this without the function onclick(event):
onclick="{formSubmit(document.getElementById("TESTForm_1010")); return false; }_gaq.push(['_trackEvent', 'Whitepaper Campaign', 'Submit Button','TESTexample.pdf'']);"
Please help! this has been racking my brain…
Instead of setting
.attr('onclick', you can bind into the event handler directly:Now clicking
#mktFrmSubmitwill fire both those click handlers.