How to leave a selected option

Asked

Viewed 1,208 times

0

How do I leave selected one option via jquery? I know it’s because of your attribute selected, 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 the text he duplicates my option, would like to leave a option with nothing, I tried using the function remove() to withdraw the class optSelecionado, but as I said I’d like to have a option blank

  • Will leave it selected by value or text ? what would be the condition to mark it as Selected?

  • would be the following situation, I have a "session" if it is in this session (which is made by the value of the option itself) I would like it even to come with the value of this session.

  • Post some code that exemplifies this please.

  • edited the question of a look

  • I posted a solution before you edit the question, now with your edition I understand even less what you want, you want to leave the input empty with the class optSelecionado selected?

  • @Felipeasunción your solution suits me

  • I just tried to exemplify what I want to do, but it’s something simple, that with the .prop() answer me

Show 2 more comments

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>
  <option value="saab">Saab Motors</option>
  <option value="opel">Opel Cars</option>
  <option value="audi">Audi Car Builder</option>
  <option value="volvo">Volvo Motorcycles</option>
</select>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.