SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing variables between functions?

    Hi all,

    This may seem fairly easy to some people but how would I pass a variable from one function into another in this code example:

    --------------------------------------------------------
    Code:
    function approve_applicant(app_id){
    
    	http.open('get', 'ajax_update.php?action=approve&app_id='+app_id);
    	http.onreadystatechange = handleProducts; 
    	http.send(null);
    }
    /* remove a single item */
    
    
    
    
    
    function handleProducts(){
    
    	if(http.readyState == 4){ //Finished loading the response
    		var response = http.responseText;
    
    		document.getElementById('app_approve_1').innerHTML = response;
    	}
    }
    ---------------------------------------------------------

    I basically need to use the 'app_id' variable used in the first function so that the page element id 'app_approve_1' used in the second variable is establed from the variable in the first function as opposed to being hard coded in there.

    Any help would be graetly appreciated.

    Thanks

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use a closure to "lock" the variable in the callee's context:

    Code:
    http.onreadystatechange = function() { handleProducts(app_id) }
    ....
    function handleProducts(app_id) { ... }

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You could also use call():
    Code:
    http.onreadystatechange = handleProducts.call(app_id)
    ...
    function handleProducts() {
      ...
    }
    where in handleProducts this is app_id. I don't know if that's better or worse in this case and would be interested to find out.

  4. #4
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah it would work to use call(), but then the app_id would become the context of the handleProducts() function, and that wouln't be especially logical.

  5. #5
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies guys... much appreciated.

    I went for sterofrogs solution and it worked fine. Will try the other suggestions soon too.

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yeah, that makes sense, wysiwyg, it makes sense to use call when dealing with objects.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •