Skip to content Skip to sidebar Skip to footer

Adding Onsubmit To A Form Via Javascript

How would you go about inserting an OnSubmit attribute to a form via Javascript only? I'm pretty new to javascript so if you're able to provide detailed example code, that would be

Solution 1:

window.onload = function() {
    var form = document.getElementById('hosted_payment_form');
    form.onsubmit = function() {
        _gaq.push(['_linkByPost', this]);
    }
}

I believe the above example is similar to what you need. We use document.getElementById to grab a reference to your form. Then set the onsubmit property to the code you want executed before the form is submitted. Remember to put this inside the window onload event if this JavaScript is executed before the page is rendered to ensure the form is built.


Post a Comment for "Adding Onsubmit To A Form Via Javascript"