JQuery set selected option for supporting multi-language

data is retrieved from web API in JSON format.

Rendered on translation data completed.

i18n.load( serviceLocation + 'api/language.php?page='+filename+'&lang='+i18n.locale, i18n.locale ).done(
function() {
  var select = $("#gender");
  select.empty().append('<option value="">'+$$("selectgender")+' *</option>');
  select.append('<option value="1">'+$$("male")+'</option>');
  select.append('<option value="2">'+$$("female")+'</option>');
  select.append('<option value="3">'+$$("other")+'</option>');

Set option from data:

function fillData(data){
  //$('#gender option:contains("'+data.gender+'")').attr('selected','selected');
  $('#gender option[value='+data.gender+']').attr('selected','selected');
}

By text:

$('#gender option:contains("'+data.gender+'")').attr('selected','selected');

By value:

$('#gender option[value='+data.gender+']').attr('selected','selected');

 

Advertisements

Leave a Reply