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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:05:22+00:00 2026-05-13T23:05:22+00:00

The question title is a bit strange because I’m not exactly sure how to

  • 0

The question title is a bit strange because I’m not exactly sure how to phrase the problem. The issue is that I have many links to which I want to bind a click event with an ajax call, and I’m just looking to refactor some duplicate code into a single area.

The links I’m trying to bind an ajax call only have one thing that differentiates them, and that’s an id from a previously declared object. So I have lots of code that looks like this:

$("a.link").bind('click', function() {
           id = obj.id; 
           $.ajax({ 
                   url: "/set/" + id, 
                   dataType: 'json', 
                   type: "POST" 
           }) 
    });

I was trying to refactor it into something like this:

$(“a.link”).bind(‘click’, ajax_link(obj.id));

 function ajax_link(id) {
      $.ajax({ 
             url: "/set/" + id, 
             dataType: 'json', 
             type: "POST" 
      }) 
 });

However, as you can imagine, this just actually makes the ajax call when the element is binded with the click event.

Is there an easy way to refactor this code so I can extract out the common ajax code into its own function, and hopefully reduce the number of lines of jQuery in my current script?

  • 1 1 Answer
  • 2 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-13T23:05:22+00:00Added an answer on May 13, 2026 at 11:05 pm

    This should do what you want:

    $("a.link").click(make_ajax);
    
    function make_ajax() {
      $.ajax({ 
        url: "/set/" + id, 
        dataType: 'json', 
        type: "POST" 
      });
    }
    

    this in this case refers to the source of the event being the link that was clicked.

    It’s also possible to define a closure:

    $("a.link").click(function() {
      make_ajax(this.id)();
    });
    
    function make_ajax(id) {
      return function() {
        $.ajax({ 
          url: "/set/" + this.id, 
          dataType: 'json', 
          type: "POST" 
        });
      };
    }
    

    but I don’t think that really helps in your example.

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

Sidebar

Related Questions

Although the question title appears a bit subjective I am sure there is not
Not even really sure what kind of title this question needs. Basically I have
I have a bit of a strange question. First let me tell you i
I'm almost sure that I do something wrong and thus the question's title is
First the apologies, i'm not sure if my question title even accuratly explains what
I don't really have an idea on how to phrase this question--even the title
The question title is a bit obscure so let me explain. A requirement that
The title pretty much frames the question. I have not used CHAR in years.
I'm sure the title of this question is a bit confusing. An example of
The title of the question might be a bit strange, but the thing is

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.