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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:33:35+00:00 2026-05-27T07:33:35+00:00

I have a series of anonymous jQuery/javascript functions for click events which have to

  • 0

I have a series of anonymous jQuery/javascript functions for click events which have to be dynamically added as a delegate function to content returned by ajax commands and i’m finding I am copy pasting a lot of code which is difficult to manage.

For example, in the code below is a delete button which is attached to all the delete buttons when the page first loads. There is also code which adds an item to a database and then adds an element to the DOM which contains a delete button. The delete event handler function needs to be added to this new button as it wasn’t there when the page first loaded, this is done through the use of the delegate function – this works perfectly well but I am having to copy paste a lot of code to achieve the delegates and it is making the code difficult to manage. Is there any way to add the delegate function without copy pasting it in?

I have tried creating a separate function for delete and calling it in the same way you would in C but couldn’t get that to work. I have also considered breaking the code up in to smaller blocks and then using php to write the javascript but that seems like a rather complex things for what I think should be simple to do. Is there another way?

Any help would be great, apologies if it is a simple fix

$(document).ready(function(){   
    $("a.delete").click(function(){
    //do stuff
    $.ajax({
        type: "GET",
        url: url,
        success: function(msg){
           //do more stuff
        }//end of success function
        });//end of ajax*/
    });//end of delete button

        $("button#addItem").click(function(){
        //do stuff
        $.ajax({
            type: "GET",
            url: url,
            dataType: "json",
            success: function(msg){
                //do stuff

                //form the id for the delete button
                var buttonID = "#delete" + msg.id;

                //need to attach the delete function to the new button to make sure the function is there
                $("body").delegate(buttonID, "click", function(){
                    //do stuff
                    $.ajax({
                        type: "GET",
                        url: url,
                        success: function(msg){
                            //do stuff
                        }//end of success function
                    });//end of ajax*/
                });//end of delegate function attaching to the delete button
            }//end of success function
        });//end of ajax
    });//end of add button
});//final close
  • 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-05-27T07:33:35+00:00Added an answer on May 27, 2026 at 7:33 am

    There are a couple of things you can do:

    Named functions

    There’s never any reason to duplicate exactly the same function in more than one place. Instead, give the function a name and refer to it from two places:

    $("a.delete").click(handleDelete);
    
    // ...
    
    $.ajax({
        // ...
        success: function(...) {
            $(some_selector).click(handleDelete);
        }
    });
    
    function handleDelete() {
        // ...do the deletion
    }
    

    And/or use event delegation

    But in many cases you can also use event delegation to hook up a handler that will get used even for elements that haven’t been created yet, using live or (if possible) delegate:

    $("a.delete").live("click", handleDelete);
    

    Or ideally, hook this up on a container that contains all of these delete links:

    $("selector_for_container").delegate("a.delete", "click", handleDelete);
    

    That tells jQuery to hook the “click” event on the document, and then when the click occurs to see if the actual element clicked matches the selector “a.delete” and if so, fire the handler.

    As of jQuery 1.7, you’d use on rather than live:

    $(document.body).on("click", "a.delete", handleDelete);
    

    Or ideally, if there’s some more specific container than the document body:

    $("selector_for_container").on("click", "a.delete", handleDelete);
    

    (Note that the order of arguments is different from delegate.)

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

Sidebar

Related Questions

Have a series of functions I am calling dynamically sort of like this: $function
I have a series of dynamically generated inputs that I need to have ajax
I have a series of divs in two columns with some javascript dictating some
I have a series of delimited files, some of which have some bad data
I have series of functions with different argument signatures: public void function1 (string s1,
I need to create a pdf file which will have series of images, where
I have a series of views which, in an editing mode, add UIButtons in
I have a series of tables which I want to all have an order
I have a series of strings, which are two numbers: 1.782-100.799 -18.107-102.016 -17.504104.059 How
I have a series of tabs that open using the jquery .animate method. They

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.