javascript to populate select
Posted: Wed Feb 13, 2008 9:16 pm
Does anyone know how i can use javascript in a joomla form (view) to populate a select box depending on the selected value in another select box?
Joomla! Community, help and support.
https://sandbox-forum.joomla.org/
Code: Select all
<tr>
<td width="100" align="right" class="key">
<label for="provinces">
<?php echo JText::_( 'Province' ); ?>:
</label>
</td>
<td>
<?php echo JHTML::_( 'select.genericlist', $this->provinces, 'provinces', null, 'id', 'title' ) ?>
</td>
</tr>
<tr>
<td width="100" align="right" class="key">
<label for="universities">
<?php echo JText::_( 'University' ); ?>:
</label>
</td>
<td>
<?php echo JHTML::_('select.genericlist', $this->universities[$selected_province_id], 'universities', null, 'id', 'title')
</td>
</tr>
Code: Select all
<script language="JavaScript">
function myJSMethod(){
//selects
var prov_select = document.forms['myform'].provinces;
var univ_select = document.forms['myform'].universities;
//selected indicies
var selected_prov_index = prov_select.selectedIndex;
var selected_univ_index = univ_select.selectedIndex;
//set univ index on prov select box, effectively make the univ select follow the prov select
univ_select.selectedIndex = selected_prov_index;
return false;
}
</script>
Code: Select all
<form name="myform">
<select name="provinces"
OnChange="myJSMethod();">
<option selected>Please Select...
Code: Select all
<script language="javascript" type="text/javascript">
<!--
var provinceuniversities = new Array;
<?php
$i = 0;
foreach ($this->universities as $province_id=>$university) {
foreach($university as $key=>$value) {
echo "provinceuniversities[".$i++."] =
new Array( '$province_id','".addslashes( $value->id)."','".addslashes( $value->title )."' );\n\t\t";
}
}
?>
//-->
</script>
Code: Select all
<tr>
<td width="100" align="right" class="key">
<label for="provinces">
<?php echo JText::_( 'Province' ); ?>:
</label>
</td>
<td>
<?php
$javascript = "onchange=\"changeDynaList( 'universities',provinceuniversities, document.adminForm.provinces.options[document.adminForm.provinces.selectedIndex].value, 0, 0);\"";
array_unshift($this->provinces, array("id"=>-1, "title"=>"Select province"));
echo JHTML::_( 'select.genericlist', $this->provinces, 'provinces', $javascript, 'id', 'title', '-1' )
?>
</td>
</tr>
<tr>
<td width="100" align="right" class="key">
<label for="universities">
<?php echo JText::_( 'University' ); ?>:
</label>
</td>
<td>
<select name="universities">
</select>
</td>
</tr>