Replace onsubmit with jquery

hd. picture hd. · Feb 25, 2012 · Viewed 24.9k times · Source

I have these code below :

<form name="submissions" action="/" onsubmit="window.open('', 'foo', 'width=900,height=900,status=yes,resizable=yes,scrollbars=yes')" target="foo" method="post">
.......
<input type="submit" id="printbtn" name="printbtn" value="Print" />
<input type="submit" id="editbtn" name="editbtn" value="Edit" />
</form>

* Edited *

the window.open should only happens when the Print button is clicked. I want to remove onsubmit() event and do it with jQuery. How can I do it?

Answer

jcreamer898 picture jcreamer898 · Feb 25, 2012

Will this work?

$('form[name="submissions"]').submit(function(event){
     event.preventDefault();
     // Do other stuff   
});

UPDATE: Actually, try this out...

$(function(){
    $('form[name="submissions"]').removeAttr('onsubmit')
        .submit(function(event){
            event.preventDefault();
                    // This cancels the event...
        });
    // This should fire your window opener...
    $('#printbtn').click(function(){
        window.open('', 'foo', 'width=900,height=900,status=yes,resizable=yes,scrollbars=yes');
    })

});

Should remove the onsubmit, and prevent the form from submitting...