I am learning Javascript for the first time. I am trying to select one value for listbox1 and based on that value it must select the value in listbox2.
This is what i coded.
<html>
<head>
function choose() {
if(document.getElementById('City').value===1) {
document.getElementById('subcity').value ="1";
document.getElementById('subcity').text ="Adyar, chennai";
}
}
</head>
<body>
<select name="City" onchange ="choose()" onclick="" id="City" >
<option value="0">Select City</option>
<option value="1">Chennai</option>
<option value="2">Bangalore</option>
</select>
<select name="subcity" id="subcity" >
<option selected="selected" value="0">Select sub area</option>
<option value="1">Adyar, chennai</option>
<option value="2">Electronic city, bangalore</option>
</select>
</body>
</html>
But it didnt seem to change or set the second listbox. It still showed me all the options i had inside it.
Can you please tell the correct way of setting values in the listbox2?
You could try using jQuery
$('#City').change(function() {
$('#subcity').val($('#City').val());
});
Put this on a separate js file and call it from the html: http://jsfiddle.net/Re8Cc/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.