How to assign the value of a combo to a value with javascript

0

I have a problem when assigning a value to a combo when I show it on my form. The code would be as follows.

//tengo mi función de donde recibo los valores desde otra parte
miFuncion=function(idorden,valor){
 $('#idorden').val(idorden);
 document.getElementById("s1").value = val(valor);
}

and in the html

<div>
 <label for="idorden">Numero de Orden</label>
 <input type="text" id="idorden" class="form-control">
</div>
<form id='tambor' >
 <select id='s1' >
  <option value='1' > opción 1 </option>
  <option value='2' > opción 2 </option>
  <option value='3' > opción 3 </option>
 </select>
</form>

Now, I can see the value of the "idorden" but it does not show me the option of the combo that it should be showing in "s1", and checking in the developer mode in the browser shows me the following error: "Uncaught ReferenceError : val is not defined ".

I thank you in advance for your help. Excellent day.

    
asked by JOSE ANGEL JIMENEZ CALDERON 08.03.2018 в 22:41
source

1 answer

0

The error is the value assignment in this line

  

document.getElementById ("s1"). value = val (value);

What you should do is keep using JQuery to set the value as you do with the input in this way.

miFuncion = function(idorden,valor){
    $('#idorden').val(idorden);
    $("#s1").val(valor);
}
//funcion de prueba
miFuncion(5,3);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div>
 <label for="idorden">Numero de Orden</label>
 <input type="text" id="idorden" class="form-control">
</div>
<form id='tambor' >
 <select id='s1' >
  <option value='1' > opción 1 </option>
  <option value='2' > opción 2 </option>
  <option value='3' > opción 3 </option>
 </select>
</form>
    
answered by 08.03.2018 / 23:07
source