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

210
Views
Change attribute of element when another element is clicked, when there are multiple elements

I'm trying to disable a textbox when a submit button is clicked - however there are multiple textboxes with submit buttons. The order of the textbox is the same as the order of the button (for example, the 1st button must disable the 1st button).

As such, I've been using the following code:

 window.onload = function() {
    document.querySelectorAll('button')[i].onclick = function fun(i) {
        document.querySelectorAll('input')[i].disabled = true
    }
}

Where i would be the order of the button/textbox.

Thanks in advance for your help.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Try using closest which should work in this case. Also I am not sure about how you iterate through the querySelectAll, but I may just not be aware of the shorthand. Change to:

window.onload = function() {
    const allButtons = document.querySelectorAll('button');
    allButtons.forEach(function(button) {
      button.addEventListener('click', function(e) {
          e.target.closest('input').disabled = true;
      });
});  
about 4 years ago · Santiago Trujillo 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!