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

105
Views
change of button's id lead to fire of click event

I try to make a click event with jquery.

The result I want is shown below:

first click - plus one

second click - plus one

third click - plus one & change of button's id and text

fourth click onward - plus one & alert pop-out

However, the alert pop-out at the third click.

my html:

<h1 id="number">0</h1>
<button id="next">Add 1</button>

my jquery:

$(document).ready(function () {
      let num = 0;

      $("#next").click(function () {
        ++num;
        if (num > 2) {
          $("#next").html("Alert");
          $("#next").attr('id', 'alert');
          $("#number").html(num);
        } else {
          $("#number").html(num);
        }

      });
      $(document).on("click", "#alert", function () {
        alert(num);
      });
    });

I don't know where I went wrong, in theory this looks fine to me but I am new to jquery and I must have missed something.

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

0

You can try following code:

$(document).ready(function () {
      let num = 0;
       $(document).on("click", "#next", function () {
        num++;
        if (num > 2) {
          $("#next").html("Alert");
          $("#next").attr('id', 'alert');
          $("#number").html(num);
        } 
        else {
          $("#number").html(num);
        }

      });
      
 $(document).on("click", "#alert", function () {
        num++;
        $("#number").html(num);
         alert(num);
        });
    });

Hope it will work.

about 4 years ago · Juan Pablo Isaza Report

0

On the third click

  • call e.stopPropagation() to prevent the #next click event being propogated to #alert element.
  • unbind the #next click event using $("#next").unbind("click"); or else $("#next").click will be triggered on clicking #alert

Also dont forget to handle the alert and increment logic inside "#alert" click event.

Working Fiddle

$(document).ready(function () {
  let num = 0;
  $("#next").click(function (e) {
    ++num;
    if (num > 2) {
      $("#next").unbind("click");
      $("#next").html("Alert");
      $("#next").attr('id', 'alert');
      $("#number").html(num);
      e.stopPropagation();   
    } else {
      $("#number").html(num);
    }
  });
  $(document).on("click", "#alert", function () {
    ++num;
    alert(num);
    $("#number").html(num);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<h1 id="number">0</h1>
<button id="next">Add 1</button>

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!