By prgrcmpny on
Information on this was hard to come by, so here is the method in the hope that it helps.
Background:we needed to provide a drop down of default values and also provide a text box to the user
if no suitable option existed in the drop down. Since the text field contained the data to store in the database, I wanted to auto-populate the text field based on the 'onchange' event in the select box.
trivial in straight html land, but it was a bit of a poser in with drupal forms--I did not immediately see how to associate javascript with a form element.
Here is how I did it.
Given a select box 'foo' and a text field 'bar', apply the following to foo's form definition.
$form['x']['foo']
'#type' => 'select',
........
'#attributes' => array('onchange'=>'duh();'),
'#suffix'=>'<script type="text/javascript">
function duh(){
var s = document.getElementById("edit-foo");
var t = document.getElementById("edit-bar");
t.value = s.value;
}
</script>',
Comments
Thanks for this example. I
Thanks for this example. I too have looked long and hard for an example of something so easy in html and so obtuse in drupal. Now perhaps my conversion to drupal will be complete...
Thank you!
I was looking for something like this, a simple solution to populate text fields according the select value. I still have to find a way to replace the 'foo' value for a database value, but your comment put me in the right way. Thank you.