EDIT: Based on your comment below, your data is as follows.
There is a problem. The HTML ID attribute must not begin with a number. Identifiers must begin with a letter.
I will show you the solution anyway, but you have to fix the identifiers.
var array = [{respID:1, respName:null}, {respID:2, respName:null}, {respID:3, respName:null}, {respID:4, respName:null}, {respID:5, respName:null} ]; $.each(array, function(i,val) { $('#' + val.respID).attr("selected", "selected"); });
Now this will give you the respID value in each iteration of the loop.
But then again, HTML identifiers cannot begin with a number. I suggest updating your HTML attributes to about id_5 instead of 5 .
<select> <option id="id_1" value="some value">some text</option> <option id="id_2" value="some value">some text</option> <option id="id_3" value="some value">some text</option> ... </select>
Then you will do the following:
$.each(array, function(i,val) { $('#id_' + val.respID).attr("selected", "selected"); });
source share