5 April 2014

Reading JSON values Example 2

<!DOCTYPE html>
<html>
<body>
<script>
var citiesJSON = '{country:[{"city":"Chennai" },{"city":"Delhi" },{"city":"Kolkata" },{"city":"Mumbai"}]}';
var obj = eval ("(" + citiesJSON + ")");
function populateValues(){
var cities = "";
for(var i=0;i<obj.country.length;i++){
cities += "<option>"+obj.country[i].city+"</option>";
}
document.getElementById("cities").innerHTML = cities;
alert("Indian cities has been populated");
}
</script>
<table>
<tr><td>
Country:</td>
<td>
<select onchange="populateValues()">
<option>Select country</option>
<option>India</option>
</select>
</td></tr>
<tr>
<td>
Cities:
</td>
<td>
<select id="cities">
<option>Select city</option>
</select>
</td></tr></table>
</body>
</html>

No comments:

Post a Comment