I am trying to pre-fill a form that containes select menu’s, using JSON, i am using the key as the #id tags for the select menu’s
but i cant seem to get it to work i have tried different selectors but still no luck,
here is my code
$(document).ready(function () {
var p = {
"weight":"39",
"height":"1.24",
"age":"34"
};
for (var key in p) {
if (p.hasOwnProperty(key)) {
if ($("#" + key + " option [value=" + p[key] + "]").length) {
$("#" + key + " option [value=" + p[key] + "]").attr('selected', 'selected');
}
}
}
});
<select id="age">
<option value="33">33</option>
<option value="34">34</option>
</select>
<select id="height">
<option value="1.25">1.25</option>
<option value="1.24">1.24</option>
</select>
<select id="weight">
<option value="38">38</option>
<option value="39">39</option>
</select>
Remove the space between selectors option and attribute selector, you should also wrap the values with quotation marks, otherwise you will get syntax errors for strings like
1.24.http://jsfiddle.net/3DLbA/