How to leave a selected option

0

How do I leave a option selected via jquery ? I know it's because of its selected attribute, but I have no idea how to do this.

The following code to illustrate how I was doing:

Session

 var sessao = localStorage.getItem('selected');
 var nomeSessao = localStorage.getItem('selectedName');
            if((sessao*1) >0)
            {
                    $('.optSelecionado').text(nomeSessao);
            }

Option Listing

function mapa()
{              $.unblockUI();
                var identificador = localStorage.getItem('identificador');
                var sessao     = localStorage.getItem('selected');
                var nomeSessao = localStorage.getItem('selectedName');
                $('.optSelecionado').text(nomeSessao);
                var url = urlprojeto+"/dispositivo/service/enterprise/apps/mapa.php";
                var www = "device="+identificador+"";
                var www = get_contents(url,www);
                var wwwD  = www.dados;
                var wwwLen  = wwwD.length;
                var ts = '<select id="caminho" style="width: 100%;">';
                    ts+= '<option class="optSelecionado" value=""></option>';
                for( s=0; s<wwwLen; s++ )
                {
                    var referencia = wwwD[s].erp_obr_ass_codigo;
                    var nome       = wwwD[s].erp_obr_ass_nome;
                    var identacao  = wwwD[s].identacao;
                    ts+= '<option value="'+referencia+'" name="'+identacao+'">'+identacao+'</option>';
                }
                ts+='</select>';
                $(".mapa").html(ts);
}
But using text it doubles my option, I would like to leave a option with nothing, I tried using the remove() function to remove the optSelecionado class, but as I'd like to have a option on white

    
asked by anonymous 06.01.2016 / 13:39

1 answer

2

Here are some examples of how to select an option:

// Selecionando o ultimo.
$("option:last").prop('selected', 'true')
  // Selecionando o primeiro.
$("option:first").prop('selected', 'true')
  // Selecionando pelo value
$("option[value=audi]").prop('selected', 'true')
  // Selecionando pelo texto
$("option:contains('Car Builder')").prop('selected', 'true')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><select><optionvalue="saab">Saab Motors</option>
  <option value="opel">Opel Cars</option>
  <option value="audi">Audi Car Builder</option>
  <option value="volvo">Volvo Motorcycles</option>
</select>
    
06.01.2016 / 13:52