select option selected option with java script

I have two select option witch are connected to each other by a script,
first one is state and second one is city, when user selects a state its related cities appear. it works great. but in next loading it shows the first one as selected option. thanks to @Tom Broucke I could handle the state but i’ confused about the city.

<select name=”state” id=”state” onChange=”iranwebsv(this.value);”>
<option value=”0″ <?php echo ( $_POST[‘state’] == ‘0’ ? ‘selected’ : ” ); ?>>choose state</option>
<option value=”1″ <?php echo ( $_POST[‘state’] == ‘1’ ? ‘selected’ : ” ); ?>>Tehran</option>
<option value=”2″ <?php echo ( $_POST[‘state’] == ‘2’ ? ‘selected’ : ” ); ?>>Gilan</option>
</select>
<select name=”city” id=”city”>
<option value=”0″>choose state</option>
</select>
<script language=”javascript”>
function iranwebsv(state)
{
with(document.getElementById(‘city’))
{
options.length = 0;

if(state == 0)
{
options[0] = new Option(‘choose state’ , ‘0’);
}

if(state == 1)
{
options[0] = new Option(‘choose city’ , ‘0’);
options[1] = new Option(‘ahmad abad’ , ‘33131’);
options[2] = new Option(‘aderan’ , ‘31541’);
options[3] = new Option(‘tapee’ , ‘18641’);
if(state == 2)
{
options[0] = new Option(‘choose city’ , ‘0’);
options[1] = new Option(‘bigtree’ , ‘43591’);
options[2] = new Option(‘asalem’ , ‘43891’);
options[3] = new Option(‘oskol’ , ‘44681’);
}
}

Read more here:: select option selected option with java script

Leave a Reply

Your email address will not be published. Required fields are marked *