0
votes

on click of button i want hi message to be displayed using javascript in drupal.I have made a .js file and know that to incude that i must use drupal_add_js(drupal_get_path('module', 'document') .'/click.js'); but the problem is to create button i used $form['click'] = array( '#type' => 'button', '#attributes' => array('onclick' =>drupal_add_js(drupal_get_path('module', 'document') . '/cancel.js')), '#value' => t('click'), );

I want that hi message which i have included in js file to be shown when button is clicked. Please help


Hi thanx for your concern.......... here is the way i proceeded in .module file

function document_form(&$node) { $form['click'] = array( '#type' => 'button', '#attributes' => array('onclick' =>message()), '#value' => t('click'), ); }

function document_form_alter(&$form, &$form_state, $form_id) { drupal_add_js(drupal_get_path('module', 'document').'/cancel.js', 'module'); $settings['click'] = array( 'nid' => $form['nid']['#value'], 'cid' => $form['cid']['#value'], 'uid' => $form['uid']['#value'], 'pid' => $form['pid']['#value'], ); drupal_add_js($settings, 'setting'); }

and my .js file code is as follows:

function message() { alert("This alert box was called"); }

<body>
</body>

but still onclick of button i m not getting the message "This alert box was called" Kindly help where the problem is coming now....... Thanx in advance.... in wait of your response

2

2 Answers

0
votes

The form alter won't add the JS file in the way you are wanting.

In the function you create the form you can use drupal_add_js, outside of the creation of the form array.

Then you can use the onclick to call the function in your JS file.

A better way to do this is to use drupal behaviours to add an click listner to the button (see example here).

0
votes

Looks like simplest solution would be, as you actually don't need the button to go to submit and other form stuff.

  1. add a link to the text

  2. style the the link as button using css .mybuttons{}

  3. hook the js on the id. $(#mybutton1).alert..