I believe jQuery will generate the DOM as follows:
<div id="date"> <select id="date"> <option value="0">- - SELECT - -</option> </select> <option value="">foo</option> <option value="">bar</option> etc... </div>
Since it automatically closes <select> after the first .append() . What you do after this adds options to the <div id="#date"/> , not the <select> that were added. I donβt think that the final closure </select> will be real either.
If you really want to use append, the following JavaScript will add options to the correct node:
// dummy data object for example var data = new Array(new Array(), new Array()); data[0].date_time = 2011; data[1].date_time = 2012; var options = new Array(); $.each(data, function(index, option) { options.push('<option value="' + index + '">'+ option.date_time +'</option>'); }); $('<select id="date"/>').append(options.join('')).appendTo('#date');
Assuming existing HTML:
<div id="date"></div>
However, this imposes overhead, since the addition occurs twice. A faster approach is to create parameter markup, as ShankarSangoli already answered
andyb source share