Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add onsubmit to form with jquery

Tags:

jquery

I am trying to add the following:

onsubmit="return processForm(this);

to this form id

<form id="formbuilder" name="featured_homes" method="post" action="fb/feedback.php">

with jquery so that the final result reads as:

<form id="formbuilder" name="featured_homes" method="post" action="fb/feedback.php" onsubmit="return processForm(this);">

Here is my attempt:

$('#formbuilder').onsubmit('return processForm(this);');

this is not working, any ideas to adjust?

like image 431
codacopia Avatar asked Jun 20 '14 21:06

codacopia


1 Answers

You actually want to add the attribute? I wouldn't do it, but here is how:

$('#formbuilder').attr('onsubmit','return processForm(this);');

WORKING JSFIDDLE DEMO

OUTPUT:

<form id="formbuilder" name="featured_homes" method="post" action="fb/feedback.php" onsubmit="return processForm(this);">

Here's the equivalent without resorting to inline JS:

$('#formbuilder').on('submit', function() {
    return processForm(this);
});

Which can also be written as:

$('#formbuilder').submit(function() {
    return processForm(this);
});
like image 166
PeterKA Avatar answered Sep 28 '22 16:09

PeterKA