How to add the selected attribute to the option tag of a select

0

I would like to know how you should add the selected attribute to the option tag in a list. I was trying several alternatives with jquery or javascript and it has not left me, I do not know what I'll be confusing about.

I leave the code so they can tell me the error.

I leave the code so they can tell me the error.

$(document).ready(function(){
$.ajax({
    "url":"lib/gestion/paciente/procesar_provincias.php",
    "type":"post",
    "dataType":"json",
    "data":{'peticion': 'procesar_provincias'}
})
.done(function(data) {
    //console.log(data.obraSocialPaciente.length);
    var nombreProvincia = $("#nombreProvincia").val();
    //console.log(nombreProvincia);
    $("#textProvincia option").remove();
    $("#textProvincia").append('<option value="0" name="0" class="">Seleccionar provincia</option>');
    for(var i = 0; i < data.provincias.length; i++){
        if(data.provincias[i].nombre == nombreProvincia){
            var id = data.provincias[i].id_provincia;
            console.log(id);
            $("#"+id).attr("selected:selected");
            //console.log(document.getElementByID(data.provincias[i].id_provincia+).value()+);
        }
        $("#textProvincia").append('<option id="'+data.provincias[i].id_provincia+'" value="'+data.provincias[i].nombre+'" >'+data.provincias[i].nombre+'</option>');
    }
})
.fail(function() {
    alert("Error al cargar las provincias");
}); 

});

The idea just that the province with id = 23 is selected

I add an image so that it is better understood

    
asked by marcos vasquez 12.11.2018 в 22:00
source

2 answers

0
  • You can add a variable that stores the ID before the For
  • cycle
  • Then within your cycle For you assign the value of the ID that you have in your IF condition
  • and outside the For cycle you can pass to your input Select #textProvince the value obtained in the previous steps

for example:

var idItemSelectedValue = ""; // variable que espera el valor seleccionado para el input #textProvincia
$("#textProvincia").append('<option value="0" name="0" class="">Seleccionar provincia</option>');
    for(var i = 0; i < data.provincias.length; i++){
        if(data.provincias[i].nombre == nombreProvincia){
            var id = data.provincias[i].id_provincia;
            console.log(id);
            //$("#"+id).attr("selected:selected");
            idItemSelectedValue = data.provincias[i].nombre; // le asignamos a la variable idItemSelectedValue el valor tomado por los elementos option dentro de un form
 //console.log(document.getElementByID(data.provincias[i].id_provincia+).value()+);
        }
        $("#textProvincia").append('<option id="'+data.provincias[i].id_provincia+'" value="'+data.provincias[i].nombre+'" >'+data.provincias[i].nombre+'</option>');
    }
    
$("#textProvincia").val(idItemSelectedValue); // asignamos el valor de la variable idItemSelectedValue
    
answered by 12.11.2018 в 22:26
-1

Briefly, did you try adding the following to your attr ?

$("#"+id).attr("selected:selected").trigger("chosen:updated");
    
answered by 13.11.2018 в 14:37