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

75
Views
Click on the button and show Alert

The following code shows an alert box:

I want each box to be displayed by clicking on its own button html

<button class="btn-alert btn-success" type="submit">delete</button>
<button class="btn-alert btn-success" type="submit">delete</button>
<button class="btn-alert btn-success" type="submit">delete</button>
<div class="alert-s col-4"></div>
<div class="alert-s col-4"></div>
<div class="alert-s col-4"></div>
<script>
      const targetDiv = document.getElementById("alert");
        const btn = document.getElementById("toggle");
         
        function clickfn() {
           for(let i = 0; i < targetDiv.length; i++) {
              if (targetDiv.style.display = "none") {
                 targetDiv.style.display = "block";
           } else {
                 targetDiv.style.display = "none";
           }
           }
          
        };
        
</script>

can anyone help me to do so?

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

0

You can add a class to all your buttons, lets say toggle, then use javascript to call your function like:

var btns = document.getElementsByClassName('toggles');

for (var i = 0; i < btns.length; i++) {
    btns[i].onclick = function () {
        alert("Finaly!");
    }
}
about 4 years ago · Juan Pablo Isaza Report

0

you don't need the for loop as getElementById returns a single element. If you want to show alert on both button click and don't want to write two separate codes give them a similar class name and query them that way using document.querySelectorAll(a dot class name in quotes).

<button class="btn">Button one</button>
<button class="btn">Button two</button>

<script>
  const btns = document.querySelectorAll(".btn");
  for (let i = 0; i < btns.length; i++) {
    btns[i].onclick = () => {
      alert(`button ${i + 1} was clicked`);
    };
  }
</script>
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!