Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8849811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:45:01+00:00 2026-06-14T12:45:01+00:00

html: <div class=box> <div><p>Hello World</p></div> </div> <input type=button id=add value=Add /> jquery: $(document).ready(function(){ var

  • 0

html:

<div class="box">
   <div><p>Hello World</p></div>
</div>
<input type="button" id="add" value="Add" />

jquery:

$(document).ready(function(){

  var count = 0;
  $("#add").click(function(){
    $(".box").append('<div id="'+count+'"><button type="button" class="close">&times;  </button></div>');
    count++;
    $(".close").click(function(){
    alert("hi");
    });
  });
});

When I click on add the buttons are appended one after the other. When I click the first button of class close with id = 0, the alert box is appearing the number of times buttons are previously added.

I mean if I append 5 buttons and when I press close whose (id = 0) then the alert box is appearing 5 times.

if id = 1 , 4 times
id = 2 , 3 times
id = 3 , 2 times
id = 4, 1 time

If I put the JQuery close click function outside the JQuery add function then the alert box does not appear at all.

How do I get the alert box to appear only once whenever I press any close button?

Hope I explained it in a way others can understand, if not I’m sorry. Any help will be appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:45:03+00:00Added an answer on June 14, 2026 at 12:45 pm

    I think you simply need to unbind the close, for example:

    $('.close').unbind('click');
    

    So at your final code this should work fine for you:

    $(document).ready(function(){
    
      var count = 0;
      $("#add").click(function(){
        $(".box").append('<div id="'+count+'"><button type="button" class="close">& times;  </button></div>);
        count++;
        $('.close').unbind('click'); //<-- This line here
        $(".close").click(function(){
        alert("hi");
        });
      });
    

    });

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello friends following is my code HTML <ul> <li> <div class=input><input name= type=checkbox value=
Consider the following code: $(document).ready(function() { $(body).append(<div class='outer'><span class='inner'>Click me</span></div>); $(html).click(function(event) { var targetClass
$('div.box').html(hello) puts the word hello inside all my div's of class 'box'. They all
$(document).ready(function() { $(#register button[name=btnPosta]).click(function() { console.log('clicked'); thisBtn = $(this); parent = $(this).parent(); name =
I have this HTML code: <div class='com_box'> <div class='com_box_c'> <div class='com_box_info'> <a id='quote'>quote</a> </div>
i have my html something like this... <div class = content> <div class =
<div class = a> <div class = a b> text </div> </div> <script> alert($(.a).html());
Fiddle is here - http://jsfiddle.net/ashwyn/a45ha/ HTML here - <div class=parent> <div class=a>Class A</div> <div
Is this html <div class='title centered'>SCE</div> <div class='description'> Magna tristique pulvinar porta montes, scelerisque
So I have some html that looks like the following: <div class='something unknown' id='something_unknown_1'>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.