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