if ($('#inputRoom option:selected').text() == null || $('#inputRoom option:selected').text() == "")
{
$('#inputRoom option').eq(0).attr('selected', 'selected');
}
i am trying to know through jquery if the user has already selected an option in my html select element. if there's still none, i would automatically set the selected value to the first option. however, this is not working.
is the way i do it is correct?
NOTE: i am populating my select element with the data i obtain from a database when the select element is loaded through $('#inputRoom').ready(....)
:
UPDATE: its working fine now... both joy and felix are correct... moreover, i found that the positioning of my knockout code causes some issue too... fixed it.. thanks...
var trueData = JSON.stringify(data).substring(10, (JSON.stringify(data).length - 2));
var rooms = trueData.split(',');
$('#inputRoom').empty();
$.each(rooms, function (index, value) {
$('#inputRoom')
.append('<option value="' + value.substring(1, value.length - 1) + '">' +
value.substring(1, value.length - 1) + '</option>');
});