Search code examples
jqueryjquery-load

jQuery .ajax load function, how to pass in data and retrieve it?


I have something like the following:

    $.ajax({
        url: "info.html?" + $(this).attr('id'),
        cache: false,
        success: function(html){
            $('#list-content').load("info.html?" + $(this).attr('id'));
        }
    })

In info.html, if I get the document.href and try to parse it, I don't get info.html?..., instead, I get the URL of the containing window which is index.html. Question, how do I get the data trailing the 'info.html?''? Is this good practice? How else can I pass in data and how can I retrieve it from info.html on document ready?

One more question, is there a way to access the elements inside info.html after loading it?

Thanks in advance.


Solution

  • Thats because ajax request are handled by the server and you are still inside index.html, which is on the client side. You can access them via $_GET in php which is the default method for load. As for accessing data in info.html, you can easily do it in a callback function after load.

    $('#list-content').load("info.html?" + $(this).attr('id'), function() {
         // js stuff here
    });