Dar Selected at a given value received in the JS function

Asked

Viewed 79 times

1

> function
> editar_etapa_projeto(etapa_projeto_id,etapa_id,dias_total,data_inicial){
>     $('#etapa_projeto_id').val(etapa_projeto_id);
>     $('#etapa_id').find('option[value='+etapa_id+']').attr('selected',true');
>     $('#dias_total').val(dias_total);
>     $('#data_inicial').val(data_inicial); }

I want with this Javascript function, that it fills the inputs of ids # such as the values received by input variables in the function.

It is working for the inputs, it takes the value of the variable, and plays in the value of the input.

But in select no,

i need it to check the value that is received in the variable etapa_id, go to the select of id #etapa_id, and a Selected in the select option where that value is.

$('#etapa_id').find('option[value='+etapa_id+']').attr('selected’,'true');

I found that it selects yes, but by error in firefox compatibility to Selected="Selected", will not.

  • 1

    You have an extra single quote after true and different quote types... Forehead with .attr('selected', true);

  • As far as I know, it’s the same rule to get select value if it’s selected, as per the @Kaduamaral response.

  • It is taking but does not select the . val

4 answers

4

Use the val similar to inputs:

$('#etapa_id').val(etapa_id);

Example:

$(document).on('click', '.setval', function(event){
	var etapa_id = $(this).data('val');
  
  $('#etapa_id').val(etapa_id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="etapa_id">
  <option value="1">Valor 1</option>
  <option value="2">Valor 2</option>
  <option value="3">Valor 3</option>
  <option value="4">Valor 4</option>
  <option value="5">Valor 5</option>
</select><br><br>

<button class="setval" data-val="1">Setar Valor 1</button><br>
<button class="setval" data-val="2">Setar Valor 2</button><br>
<button class="setval" data-val="3">Setar Valor 3</button><br>
<button class="setval" data-val="4">Setar Valor 4</button><br>
<button class="setval" data-val="5">Setar Valor 5</button><br>

Jsfiddle

  • I don’t want to take the value of select, I want to take an existing value without adding a new one to it and leave it selected already, this does not select

  • If there is a option with the value equal to the variable value etapa_id in the select#etapa_id the answer expression will set the option as selected: jsfiddle

  • @Andrésilveiramachado the method val jQuery, when you do not pass a parameter val() it returns the value of the element, when you pass val("algumvalor") it arrow the value of the element. More details in the documentation.

0

Missing update select for firefox to work

    $('#etapa_id').selectmenu({ }); 

0

Missed ", see how it should be:

var el, valor;
el = $('#etapa_id').find('option[value="'+etapa_id+'"]')
el.attr('selected’,true');
valor = $('#etapa_id').val();

0

Doing so will already work:

    function editarEtapaProjeto(data) {
        for (var i in data) {
             $('#'+data[i][0]).val(data[i][1]);
        }
    }

    var data = [
                {'etapa_projeto_id', 1},
                {'etapa_id', 2},
                {'dias_total', 10},
                {'data_inicial', '2015-10-10 20:00:00'}
               ];
     editarEtapaProjeto(data);

Obs: I reformatted the code to follow a formatting pattern.

Browser other questions tagged

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