I have a jquery ajax call that will be pulling back json data such as this:
{
"Title":"The Office",
"Season":"1",
"totalSeasons":"9",
"Episodes":[
{
"Title":"Pilot",
"Released":"2005-03-24",
"Episode":"1",
"imdbRating":"7.6",
"imdbID":"tt0664521"
},
{
"Title":"Diversity Day",
"Released":"2005-03-29",
"Episode":"2",
"imdbRating":"8.3",
"imdbID":"tt0664514"
},
{
"Title":"Health Care",
"Released":"2005-04-05",
"Episode":"3",
"imdbRating":"7.9",
"imdbID":"tt0664517"
},
],
"Response":"True"
}
From that data I wish to build an html select bound to it. For example:
<select>
<option value="tt0664521">Pilot - Episode 1</option>
<option value="tt0664514">Diversity Day - Episode 2</option>
<option value="tt0664517">Health Care - Episode 3</option>
</select>
My js code will often (though not always) know the episode number already, which maps to the "Episode" data element in the json. When I know the episode, I want to pre-select that option within the select. If I don't know it, no pre-select. In all cases I want to create an option for each record. This has to happen dynamically in the js because I won't be able to get back this json until the user enters some data.
I have the ajax calls all working, I just need to know how to dynamically add options to my select bound to that data, and then to auto-select an option when I know the episode.
A pure jquery solution would be ideal, but I'm happy with just js also.
How is it done? (And thank you!)
Note: This is not a duplicate of this post. In my question here, binding to json data is a key piece of the puzzle.