0

i'm try set selected with javascript and jquery but dont set the value, my code:

<select class="form-control" id="destino" name="destino" required>
</select>

this is my select for append the options i use a cycle for:

for(i=0; i<subdirectorios.length; i++){
 temporal_value =path_directorioPrincipal.concat(subdirectorios[i]);
 var option = $("<option value="+temporal_value+">"+ subdirectorios[i] +"</option>");

  //append option
  $("#modalProceso #destino").append(option);

}

the output: output i don't understand why set value as :

value="media/gestionDocumental/Operativos/Prueba" produccion=""

in the console return this value:

media/gestionDocumental/Operativos/Troquelado
media/gestionDocumental/Operativos/Prueba Produccion

for set selected use:

$('#destino option:contains("' + destino_path + '")').attr("selected", true)
//or this:
$('#destino').val(destino_path);

but dont set selected. please any suggest..thanks..!!

Diego Avila
  • 700
  • 7
  • 24
  • https://stackoverflow.com/questions/13343566/set-select-option-selected-by-value – Adam H Aug 02 '18 at 17:27
  • https://stackoverflow.com/questions/36128440/how-to-preserve-space-in-html-select-option-listoption-value-hi-thishi-opti I think this might help. – Manita Pote Aug 02 '18 at 17:27

1 Answers1

0

Firstly - You need to wrap your options value in quotes otherwise when space occurs it'll be parsed as a separate property.

$("<option value='"+ temporal_value +"'>"+ subdirectorios[i] +"</option>");

As for your second issue you actually should call .change() after setting the option value, like below:

$("#destino").val(destino_path).change();

Sample JSFiddle

Adrian
  • 8,271
  • 2
  • 26
  • 43
  • thanks you solve my problem on set values for options only the problems is set seleted..by consele insert this: $("#destino").val('media/gestionDocumental/Estrategicos/calidad'); – Diego Avila Aug 02 '18 at 17:46
  • and set selected but by my javascript file dont set selected – Diego Avila Aug 02 '18 at 17:46