Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

95
Vistas
slider.onchange not triggered without clikcing?

I'm a beginner of js. I wanna update a text as long as the value of a slider changes. Since the slider might be changed without click, I decide to use .onchange to detect if the value of the slider is changed. However, it doesn't work. The text is only be updated if the change of the slider is made by clicking. I'm wondering if there is a good way to detect any change of the slider? Thanks!

btw I also have tried the .oninput but I got the same result:(. So here is my current code

// definition of slider and p in HTML
<input type="range" min="0" max="100" id="slider1" value="0"><p id=p1>0</p><br />
...
// In js, so the slider can affect each other
    slider1.onclick = function(){
        slider3.value = v2>v1?(v2-v1):(v1-v2);
    }
    slider2.onclick = function(){
        slider3.value = v2>v1?(v2-v1):(v1-v2);
    }
    slider3.onclick = function(){
        slider1.value = (slider3.value+slider2.value)%100;
    }
// I wanna change the corresponding innerHTML when the value of slider is changed. 
// However, the onchange isn't been triggered when the slider.value is changed
    slider1.onchange=function(){
        p1.innerHTML=slider1.value;
    }
    slider2.onchange=function(){
        p2.innerHTML=slider2.value;
    }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In order to reference an HTML element from javascript, you must get a reference to it: setting the id from an HTML element allows us to identify it, but we must use the document to actually refer to it.

<input id="slider1" type="range" />

JS:

let slider1 = document.getElementById("slider1");
// Repeat for slider2 and slider3
let p1 = document.getElementById("p1");
// Repeat for p2 and p3

slider1.onclick = function(){
    slider3.value = v2>v1?(v2-v1):(v1-v2);
}
// Rest of your code
</script>

See the Mozilla documentation on document.getElementById() for more info: https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda