Hi all, How to add onsubmit attribute to form. I need to run a js function and custom js validation function before submitting. How can I set the onsubmit attribute

Comments

drupaling.in’s picture

found the answer : $form['#attributes']['onsubmit'] = ' return func();';

takinola’s picture

I have tried using the solution above

$form['#attributes']['onsubmit'] = ' return func();';

and unfortunately it does not work for Drupal 6.x

Do you know of a solution for Drupal 6?

cj-a-min’s picture

You have to put it in your own custom module:

function mymoduletweaks_form_alter(&$form, &$form_state, $form_id){
       if($form_state...........){
           $form['#attributes']['onsubmit'] = 'return func();';
       }
}
pritam.tiwari’s picture

You can try this code. You can add other attributes which are separated by ','

 $form['#attributes'] = array('class' => 'my_class','onsubmit' => 'return func();' );

I hope this will be useful to you.