I'm attempting to alert the keys (NOT values) of an object specified by a user selected value but I'm having no luck. I can successfully alert the keys if I manually input the object name in the for loop. Heres the snippet of my code:
<select id="state" class="pure-input-1-2" onchange="populateAirport(this.value)">
<option value = ""></option>
<option value = "AL">AL</option>
<option value = "AK">AK</option>
</select>
function populateAirport(selectedValue){
AL = {"Birmingham–Shuttlesworth International Airport":"BHM","Huntsville International Airport":"HSV"}
AK = {"Ted Stevens Anchorage International Airport":"ANC","Fairbanks International Airport":"FAI","Juneau International Airport":"JNU","Ketchikan International Airport":"KTN"}
for(var i in selectedValue){//if I replaced 'selectedValue' with 'AL' I alert the keys in the object just fine
alert(i);
}
}
I just want to be a to dynamically reference an object based on the selected value. Thanks in advance for your help.
selectedValuein a for loop the way you are doing it, it assumesselectedValueis an array. In reality, your selectedValue is just a string "AL", "AK".. thats the problem.. what are you trying to do with this loop? Im assuming, what you are trying to do is when the user changes the select dropdown, then you get the contents of the object.. but what do you want to do with the object? Not to mention, you also have not declared thoseAL,AKvariablesALandAKare JavaScripts objects. Nothing you posted is related to JSON in any way.