Scoping inside Javascript anonymous functions

Posted by DCD on Stack Overflow See other posts from Stack Overflow or by DCD
Published on 2010-04-28T17:13:54Z Indexed on 2010/04/28 17:23 UTC
Read the original article Hit count: 352

I am trying to make a function return data from an ajax call that I can then use. The issue is the function itself is called by many objects, e.g.:

function ajax_submit (obj)
{   
    var id = $(obj).attr('id');
    var message = escape ($("#"+id+" .s_post").val ());

    var submit_string = "action=post_message&message="+message;

    $.ajax({  
        type: "POST",  
        url: document.location,  
        data: submit_string,  
        success: function(html, obj) {
            alert (html);
        }  
    }); 

    return false;
}

Which means that inside the anonymous 'success' function I have no way of knowing what the calling obj (or id) actually are. The only way I can think of doing it is to attach id to document but that just seems a bit too crude. Is there another way of doing this?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery