0

I have a loop that loops through each item in an array and populates a check box.

the identity alternates throughout the loop.

"[{\"NAME\":\"TOM, B\",\"ID\":\"222\"},{\"NAME\":\"joe\",\"ID\":\"202\"}]"

how can I get the text of the select box to be the ID and the Name to be the text.

$.each($.parseJSON(json), function () {               
   $.each(this, function (key, value) {
     drname.options.add(new Option(value, value[+1])); 
    });
});

I tried the [+1] as I've seen in similar questions but no dice.

thanks.

1 Answer 1

5

There is no need for that second $.each, just use the properties of the object directly without iterating through them:

$.each($.parseJSON(json), function(){               
    drname.options.add(new Option(this.ID, this.NAME)); 
});

Depending on what order the Option constructor expects the argument, it could also be:

new Option(this.NAME, this.ID)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. A point worth mentioning... every time this function is called the options are appended to the list. For a second I thought something was wrong because my select box was three times longer than I expected. But that was only because I called the function three times. I used $("selectBoxName").empty() before the function to clear the options of the select box because in my situation the function may be called more than once with a json array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.