Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- area[0].states[0]
- area[0].cities[0]
- var area = [
- {
- "State" : "Texas",
- "Cities" : ['Austin','Dallas','San Antonio']
- },
- {
- "State" :"Arkansas",
- "Cities" : ['Little Rock','Texarkana','Hot Springs']
- }
- ] ;
- area = {
- "Texas": ['Austin','Dallas','San Antonio']
- }
- states = {}
- for(var j=0; j<area.length; j++)
- states[ area[0].State ] = area[0].Cities
- states["Texas"]
- ['Austin','Dallas','San Antonio']
- var area =
- {
- "Texas" : { "Cities" : ['Austin','Dallas','San Antonio'] },
- "Arkansas" : { "Cities" : ['Little Rock','Texarkana','Hot Springs'] }
- };
- area["Texas"].Cities[0];
- <select id="states" size="2"></select>
- <select id="cities" size="3"></select>
- var area = [
- {
- "states" : "Texas",
- "cities" : ['Austin','Dallas','San Antonio']
- },
- {
- "states" :"Arkansas",
- "cities" : ['Little Rock','Texarkana','Hot Springs']
- }
- ] ;
- $(function() { // create an array to be referenced by state name
- state = [] ;
- for(var i=0; i<area.length; i++) {
- state[area[i].states] = area[i].cities ;
- }
- });
- $(function() {
- // populate states select box
- var options = '' ;
- for (var i = 0; i < area.length; i++) {
- options += '<option value="' + area[i].states + '">' + area[i].states + '</option>';
- }
- $("#states").html(options); // populate select box with array
- // selecting state (change) will populate cities select box
- $("#states").bind("change",
- function() {
- $("#cities").children().remove() ; // clear select box
- var options = '' ;
- for (var i = 0; i < state[this.value].length; i++) {
- options += '<option value="' + state[this.value][i] + '">' + state[this.value][i] + '</option>';
- }
- $("#cities").html(options); // populate select box with array
- } // bind function end
- ); // bind end
- });
- var area = {
- "Texas" : ["Austin","Dallas","San Antonio"],
- "Arkansas" : ["Little Rock","Texarkana","Hot Springs"]
- };
- // area["Texas"] would return ["Austin","Dallas","San Antonio"]
Add Comment
Please, Sign In to add comment