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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:53:23+00:00 2026-06-09T22:53:23+00:00

I’m having issues with some code I’m working on. I have a generic function

  • 0

I’m having issues with some code I’m working on. I have a generic function (ajaxContent) that is used from several points. What I need for this is a to run a function after the AJAX call has been made, but I need to pass though some data that I have before I made the call (so not data that comes back from the AJAX call.

function ajaxContent(url, id, target, data, callbackFunction) {

  $.ajax({
    async : true,
    ....    
    complete: function(){
      if(typeof callbackFunction == "function"){
        callbackFunction(); // function get's executed here, lead_id is not available
      }
    }
  });
}    

$(document).on("click", "#something", function(){
  var leaf_id = $(this).attr('rel');

  ajaxContent(url, id, target, data, function(leaf_id){

    url = "/path/" + leaf_id;
    window.location = url;

  })
})

$(document).on("click", "#action", function() {

    ajaxContent(url, id, target, data, function(){

        if ($(this).attr('rel') == "M_0") // $(this).attr('rel') not available here, so need to pass through some way
            $('#treedata').css('background', 'url(/img/rotonde.png) 50% 50% no-repeat');
  });

});    

I could perhaps use somehing like

function ajaxContent(url, id, target, data, callbackFunction, varA, varB, varC, varD) { 
   callbackFunction(varA, varB, varC, varD);
} 

with

ajaxContent(url, id, target, data, function(varA){
}, varA, varB, varC, varD)` 

But not really scalable.

I hope this is clear 🙂

  • 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-09T22:53:25+00:00Added an answer on June 9, 2026 at 10:53 pm

    In the code you have in your answer now, leaf_id is a local variable inside the document.ready() handler function. As such, it is not available outside that context.

    You have several choices to fix that:

    1. You can move the definition of ajaxContent function inside the document.ready() handler function so that it will be able to see leaf_id.

    2. You can make leaf_id a global variable so it can be accessed from anywhere.

    3. You can pass leaf_id to ajaxContent as a function argument.

    Without more context about what you’re doing, and because this is a transient value that is associated only with the particular action, I think the best option is #3:

    function ajaxContent(url, id, target, data, leafID, callbackFunction) {
    
        $.ajax({
            async : true,
        ....        
            complete: function(){
              if(typeof callbackFunction == "function"){
                // you can access any of the ajaxContent function arguments here
                callbackFunction(leafID);
              }
            }
        });
    }       
    
    $(document).on("click", "#something", function(){
      var leaf_id = $(this).attr('rel');
    
      ajaxContent(url, id, target, data, leaf_id, function(leafID){
    
        url = "/path/" + leafID;
        window.location = url;
    
      })
    })
    

    Or, if you have lots of variables to pass through, you can create an empty object and then put each variable as a property on that object and then just pass the single object into ajaxContent and into your callback like this:

    function ajaxContent(url, id, target, data, settings, callbackFunction) {
    
        $.ajax({
            async : true,
        ....        
            complete: function(){
              if(typeof callbackFunction == "function"){
                // you can access any of the ajaxContent function arguments here
                callbackFunction(settings);
              }
            }
        });
    }       
    
    $(document).on("click", "#something", function(){
      var args = {};
      args.leafID = $(this).attr('rel');
    
      ajaxContent(url, id, target, data, args, function(settings){
    
        // access args.leafID here
        url = "/path/" + leafID;
        window.location = url;
    
      })
    })
    

    Note, in my code example, I named the argument something different than the other variable to avoid confusion about which is which.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT

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.