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

192
Vistas
How to change the value of a attribute in a class?

I have this javascript code :

<input class="inp " type="text" value="0.01" inputmode="numeric">

I want to change the attribute value but nothing i tried worked . I tried this metrods :

document.getElementsByClassName("inp")[0].value=0.02;
document.getElementsByClassName("inp")[0].setAttribute("value","0.02");
document.querySelector("input.inp").value=0.02;

None of these seem to work they just change the value on the surface or the hud but when i try to use the changed value its always the value before i made the changes.

Am i doing something wrong or the site just restricts the changes of that attribute ?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You have a strong misconception here. Your code works fine, the problem is your expectations when modifying the value attribute.

The value attribute (as opposed to the value property!) serves only a single purpose: Declaratively defining the initial value in the HTML. With any Javascript you typically never interact with the value attribute.

Changing it later will not change the displayed value (but it will change el.defaultValue), nor will changing the value by interacting with the element update the attribute.

const el = document.querySelector("input.inp");
console.log(`el.value: ${el.value}, <input value="${el.getAttribute('value')}">, el.defaultValue: ${el.defaultValue}`);

el.value=0.33;
console.log(`el.value: ${el.value}, <input value="${el.getAttribute('value')}">, el.defaultValue: ${el.defaultValue}`);

el.setAttribute("value","0.66");
console.log(`el.value: ${el.value}, <input value="${el.getAttribute('value')}">, el.defaultValue: ${el.defaultValue}`);
<input class="inp" type="text" value="0.01">

about 4 years ago · Juan Pablo Isaza Denunciar

0

After testing your code, it works correctly, your issue seems to be outside of the code.

All of these work correctly.

document.getElementsByClassName("inp1")[0].value=0.02;
document.getElementsByClassName("inp2")[0].setAttribute("value","0.02");
document.querySelector("input.inp3").value=0.02;
<input class="inp1" type="text" value="0.01" inputmode="numeric">
<input class="inp2" type="text" value="0.01" inputmode="numeric">
<input class="inp3" type="text" value="0.01" inputmode="numeric">

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