1

I have a variable () loaded with a html code:

$.get(url, function (res, data) {
    response = res.responseText;
}

The response variable gets loaded with this:

<html>
<head>
</head>

<body>
    <div id="version">1</div>
</body>

</html>

how can I retrieve the content of the div#version to a variable version?

Thanks

3 Answers 3

6

Since you are using jQuery and the response is html, then you can simply do this:

var version = $(res.responseText).find('#version').html();
Sign up to request clarification or add additional context in comments.

2 Comments

looks good and accurate. should be var version instead of var r
I think that this is more helpful than @Sirko's answer. "filter" didn't work for me.
5

If you want jQuery to parse your result resulting HTML and just retrieve the content of #version you can use the following:

var response = '<html><head></head><body><div id="version">1</div></body></html>';

var version = $( response ).filter( '#version' ).text();

Edit: Refer to @freakish, whose is the better answer.

Comments

0

Try this -

var version = document.getElementById("version").innerHTML;

or if you are using jquery then try this -

var version = $("#version").text();

1 Comment

This only works when you actually add the HTML to body. I don't think this is what OP wants.

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.