Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!