My problems is that I have a drop down list and its values populated with Ajax from a jQuery call. How can I make a specific option set as the default selected option?
<li>
<label>Type</label>
<select name="type"class="TypeSelector"></select>
<span class="errorMessage"></span>
</li>
$.getJSON("http://localhost/Mar7ba/Type/getAllTypes/TRUE",function(data){
var options = '';
options+="<option>Select Type</option>";
for(var i=0;i<data.length;i++){
options += "<option>"+data[i]+"</option>";
}
if($("#addPlace #apTypeSelect").length){
$("#addPlace #apTypeSelect").html(options);
}
if($('.TypeSelector').length){
$('.TypeSelector').html(options);
}
});
and the options selected is
<?php echo $question['name'];?>
I tried like this:
<li>
<label>Type</label>
<select value="<?php echo $question['name'];?>"name="type"class="TypeSelector"></select>
<span class="errorMessage"></span>
</li>
...but doesn't work what am i doing wrong ?
Do it via jQuery. Give your select
an ID (I've used mySelect
) and do:
$(function() {
$("#mySelect").val("<?php echo $question['name'];?>");
});