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 7733647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:03:03+00:00 2026-06-01T07:03:03+00:00

So I wrote the code below: jQuery(‘#contentWrapper’).delegate(.addButton, click, function(){ addRow(jQuery(this)); }); function addRow(thisButton){ var

  • 0

So I wrote the code below:

   jQuery('#contentWrapper').delegate(".addButton", "click", function(){
      addRow(jQuery(this));
   });

function addRow(thisButton){

        var parent = thisButton.parent();  
        console.log("parent = ",parent);

        var childrenNoEvents = thisButton.parent().children().clone(false); 
        console.log("childrenClone = ",childrenNoEvents);

        var cloneWithoutEvents = thisButton.parent().clone(false);
        console.log("parentClone = ",cloneWithoutEvents);

        thisButton.parent().append(childrenNoEvents);
    };

which operates on an DOM element like this

<div class="whatever">
    <span>first element</span><span class="addButton">O</span><span class="deleteButton">X</span>
</div>

When I click the O(the DOM element with the addButton class) the first time it works properly, but subsequent times it creates/clones twice as many elements as the prior click. I assume it has to do with the delegate handler calling addRow for as many “.addButton” classes there are in the DOM but I don’t know how to fix it.

Bonus Question:

How would I use a closure to create a persistent variable that I can increment inside the addRow function each time the event is fired? (or is there a better way to do that?)

  • 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-01T07:03:12+00:00Added an answer on June 1, 2026 at 7:03 am

    Everytime you execute the function, you copy the contents of .whatever back into .whatever, effectively doubling them. Try using this approach:

    $('#contentWrapper').on("click", ".addButton", function() { // note that I use the .on() function, as .delegate() is just a wrapper since jQuery 1.7
        addRow($(this));
    });
    
    function addRow(thisButton){
        thisButton.closest('ul').append(thisButton.parent().clone(false));
    };
    

    With HTML syntax like this:

    <div id="contentWrapper">
        <ul class="whatever">
            <li>
                <span>first element</span>
                <span class="addButton">O</span>
                <span class="deleteButton">X</span>
            </li>
        </ul>
    </div>
    

    You can test it with this JSFiddle: http://jsfiddle.net/ZsCCs/

    More info about the .on() method: http://www.elijahmanor.com/2012/02/differences-between-jquery-bind-vs-live.html

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

Sidebar

Related Questions

I have the below jQuery code: $(document).ready(function () { $('#ajaxButton').click(function () { $.ajax({ url:
I wrote a PHP code for prepare jQuery function, which can select multipal values
I want to include jquery.js in myjs.js file. I wrote the code below for
I have below code in Jquery $(document).ready(function() { // bind to cells with an
I wrote the code below : 1. MyClass[] origArr=new MyClass[3]; 2. MyClass[] arr1; 3.
I have wrote the code below which was taken from Java How to program
Below is the code which I wrote to display a tooltip content. As the
I wrote code to set permission of the folder. Function I developed was public
I wrote code that fails to use the JQuery autocomplete to fire a result
I want to use jQuery.getJSON function in ASP.NET MVC 3.0, so I wrote the

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.