Alex Rivera | Logout

change input text value with js or jQuery before submit if value is null

Asked 2012-03-30T13:30:22.020
18

How can I with js or jQuery change input text value with js or jQuery before submit if the input value is null ?

Thanks for help.

Edit
Report

1 Answer

40

With jQuery, you can bind a callback to the submit event with the .submit() method.

$("form").submit(function(){
   // Let's find the input to check
   var $input = $(this).find("input[name=whatever]");
   if (!$input.val()) {
     // Value is falsey (i.e. null), lets set a new one
     $input.val("whatever you want");
   }
});

Notice: To ensure that all the elements can be found it should be placed in a domready function.

answered 2012-03-30T13:32:51.230

Your Answer