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

  • Home
  • SEARCH
  • 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
  • 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-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

I'm not really sure how to title this question but basically I have an
The title might be worded strange, but it's probably because I don't even know
The original question is below, but I changed the title because I think it
I know the title is a little bit strange, but here is what the
I'm a bit confused about mvc terminology. Using the question title as an example,
This question may seem a little bit stackoverflow-implementation specific, but I have seen a
I know the title of the question is a bit odd. But I don't
Sorry for the strange title. Here is my situation. I have a table of
OK, the question title is a bit crappy, but I didn't really know how
I know the title is bit confusing for my question. Let me explain:- There

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.