Quantcast
Channel: Usuario ontimond - Stack Overflow en español
Viewing all articles
Browse latest Browse all 20

Respuesta de ontimond en ¿Cómo cambiar un label cuando se seleccione una opción en un select?

$
0
0

Yo recomendarías que utilizaras el atributo value de la opción seleccionada en vez de text ya que puedes cambiar el texto para el usuario sin alterar la funcionalidad.

function ShowSelected() { /* Para obtener el valor */  var cod = document.getElementById("producto").value;  var txt = document.getElementById("lbl").value;    /* Para obtener el texto */  var combo = document.getElementById("producto");  var selected = combo.options[combo.selectedIndex].value;    if (selected == "ropa") {    document.getElementById('lbl').innerText = '10000';  } else {    document.getElementById('lbl').innerText = '';  }}
<html><head><title></title></head><body><select id="producto" onchange="ShowSelected();" name="producto"><option value="">Seleccionar</option><option value="ropa">ropa</option><option value="zapatos">zapatos</option></select><label name="lbl" id="lbl"></label></body></html>

EDICIÓN

Podrías guardar el valor que vas a mostrar en el label en el valor de la opción del select

function ShowSelected() { /* Para obtener el valor */  var cod = document.getElementById("producto").value;  var txt = document.getElementById("lbl").value;    /* Para obtener el texto */  var combo = document.getElementById("producto");  var selected = combo.options[combo.selectedIndex].value;      document.getElementById('lbl').innerText = selected;}
<html><head><title></title></head><body><select id="producto" onchange="ShowSelected();" name="producto"><option value="">Seleccionar</option><option value="10000">ropa</option><option value="20000">zapatos</option></select><label name="lbl" id="lbl"></label></body></html>

Viewing all articles
Browse latest Browse all 20

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>