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

232
Views
Why some operations do not apply to an element with two CSS classes?

I have two input elements and a button as follows:

<input class="close-acc-usr opt-in" type="text" name="closeAccUsr" />
<input class="close-acc-pin opt-in" type="number" name="closeAccPin" />
<button class="opt-btn close-btn">→</button>

In js I want to by clicking on the button, both input element contents become clear, so I write this line of code in the button event handler function:

document.querySelector('.opt-in').value = '';

but it does not work!! in fact if I write above code for '.close-acc-pin' and '.close-acc-usr' class names instead of .opt-in it works! but why with '.opt-in' it does not work?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Try to select all inputs with querySelectorAll :

const btn = document.querySelector('.opt-btn')
btn.addEventListener('click', function() {
  const inputs = document.querySelectorAll('.opt-in');
  inputs.forEach(input => input.value = '')
})
<input class="close-acc-usr opt-in" type="text" name="closeAccUsr" />
<input class="close-acc-pin opt-in" type="number" name="closeAccPin" />
<button class="opt-btn close-btn">→</button>

about 4 years ago · Juan Pablo Isaza Report

0

Instead of using two classes for that input why not change that opt-in class into an id like this:

<input id="opt-in" class="close-acc-usr" type="text" name="closeAccUsr" />
→

Then in your JS this:

document.querySelector('#opt-in').value = '';

or better still this

 document.getElementById('opt-in').value = '';
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!