Ver Mensaje Individual
  #3 (permalink)  
Antiguo 13/10/2016, 14:22
alvaro_trewhela
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Escribir dentro de un campo select

No le veo la utilidad jajaja pero bueno.. No creo y no conozco un método para escribir EN el select, pero si escribir en un campo texto, y pasarlo...

Como dijo el camarada de arriba hay que usar appendchild... Te dejo un ejemplito sencillito para entenderlo, aplique la funcion see() para ver si tomaba bien el value del option recien creado...

Código HTML:
Ver original
  1.     <script type="text/javascript">
  2.     function add(){
  3.     var sel = document.getElementById("sl");
  4.     var add = document.getElementById("adder");
  5.    
  6.     var option = document.createElement("option");
  7.     option.appendChild(document.createTextNode(add.value));
  8.     sel.appendChild(option);
  9.     option.setAtrribute("value", add.value);
  10.     }
  11.    
  12.     function see(){
  13.     alert(document.getElementById("sl").value);
  14.     }
  15.     </script>
  16. </head>
  17.  
  18. <select id="sl">
  19.     <option value="A">A</option>
  20.     <option value="B">B</option>
  21.     <option value="C">C</option>
  22. </select><br/>
  23.  
  24. <input type="text" id="adder" /><button onclick="add();">Agregar opción</button><br/>
  25. <button onclick="see()">Ver</button>
  26.  
  27. </body>
  28. </html>

Saludos,