1

I need to output some javascript codes as well as form fields.

I am using Drupal 6 and form API to build the form. How can I output those javascript codes? Is there a 'javascript' type like textfield/checkboxes ?

I have tried the code below but string 'Array' is printed on the page instead of js code:

$form['target'] = array(
'#prefix' => '<div id="newuser">' ,
'#value' => drupal_add_js('alert("hello)','inline'),
'#suffix' => '</div>'

);

2

drupal_add_js() is for adding script tags to the page. It doesn't go in a form element.

 drupal_add_js('alert("Hello!")', 'inline');

would go by itself on a line. This would add that javascript to the page, so when the page loads you would get a pop-up that says "Hello".

and/or to add markup to the page you would do this:

$form['target'] = array(
  '#prefix' => '<div id="newuser">',
  '#value' => t('Hello'),
  '#suffix' => '</div>'
);

Which would add the text "Hello" to the form in the newuser div.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.