Question

Like my title says, is it possible to use JQuery to access elements found in a return "data" object from $GET or $POST?

    $.get( "ajax/test.html", function( data ) {
    });

Let's say the data from $.GET is this:

<h1>hey world</h1>
<p id='first'>first item</p>

Could I access the info with something like this?

var item = $(data).('#first').html();
Was it helpful?

Solution

You should do this inside that callback since the call is async, this ensures you have access to the data right when it comes back:

$.get( "ajax/test.html", function( data ) {
    var item = $(data).filter('#first').html();
});

OTHER TIPS

You should use filter():

var item = $(data).filter('#first').html();

You have to use .filter() to filter out the required element from the element collection. Please read here for more reference.

Try this,

var item = $(data).filter('#first').html();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top