views:

34

answers:

2

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

A: 

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).

Jeremy French
A: 

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

ruhi
What are you setting $setting to?
Jeremy French
This idea i got it from your link "example here" in your answer.As you said i added drupal_add_js() in function document_form_alter().still not getting desired result.Help......how to proceed now
ruhi
I inserted jquery inside my .js fileDrupal.behaviors.alertTest = function() { alert("foo"); };and gave a call to this via $form['click'] = array( '#type' => 'button', '#attributes' => array('onclick' =>Drupal.behaviors.alertTest), '#value' => t('click'), );and put the drupal_add_js() inside hook_form_alter()doing this i m getg d alert message on click button but also getting the same message on load of the module(which is not desired).Kindly help Thanx in adv
ruhi

related questions