Getting a form element without a form

Last modified: May 17, 2009 - 01:01

I'm using AJAX to replace form elements within a form and I need to get a form element, such as a select box, without the surrounding form. Here's how!

Build your $form like normal, then call this:

<?php
print form_render(form_builder('my_select', $form));
?>

Correction for Drupal 5

Note that you should use drupal_render() for Drupal 5, NOT form_render().

When outputting as JavaScript code, you could do something like this:

<?php
$form_item
= $form['some_form_item'];
$js_form_item = drupal_to_js(drupal_render($form_item));
$js_code = "var some_form_item = $js_form_item;";
?>

 
 

Drupal is a registered trademark of Dries Buytaert.