As I say in my question.
What happens is that through Ajax I make a call to an HTML document, but I only want to obtain a certain element, let's say only the content of the body.
How can I achieve it?
Well what I'm trying to do is implement navigation using javascript's 'History' api, and I want to get only a specific content and put it in a specific div.
Since you don't always have control over the ids that may be on other pages, it might be interesting for you to use
$.get
.Thus, you can work with the answer. You can create a DOM with the response using DOMParser and access the body via
elementoDOM.body
, or any element without having to rely on it having an id or some selector.Let's see a test, putting the
body
of the response in thediv
whose id isresultado
:Now an example using
$.ajax
:NOTE:
In the specific case of
body
, if you do this:will double the
body
.You should use in that case:
You can use Jquery's load function to get the content of an external html page and add the content to a div of, for example, the current document:
You need to add an identifier to the part of the other document that you want to access, in the example #bodydeldocumento, it refers to:
Using ajax it would look something like this: