0

When making a call to jQuery get, how does one specify HTML elements to be returned?

2 Answers 2

3

An AJAX call doesn't return DOM elements. It sends an asynchronous HTTP request to a server side script which returns a result which could be of any type HTML, XML, JSON, .... This result is then passed to the success callback for processing:

$.get('/script', function(result) {
    // TODO: do something with the result returned by the server script
});

So for example if your server side script returned some HTML content and you wanted to extract a particular element you could do this:

$.get('/script', function(result) {
    // get an element with id="foo" from the returned result
    var foo = $(result).find('#foo');
});
Sign up to request clarification or add additional context in comments.

Comments

2

Darin is right. However, you might be looking for the functionality provided by load (the Ajax method not the event handler).

Something like $('#result').load('ajax/test.html #container'); will parse the response HTML for the element with the id container and insert it into the HTML of result.

You could maintain an invisible div and extract specific DOM elements out of it after load (load)() also accepts a callback that will be called when the load is finished).

$('#result').load('ajax/test.html #container,.classname', 
                   function(){
                      $('#result').html(); //this should give you the DOM that matches either .classname or #container
                  });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.