Trying to convert a jQuery get request to a processed object

I am trying to parse a jQuery get request into an object or some other way to get a specific div on a page.

Here is my code:

  $.get("http://en.wikipedia.org/wiki/Afghanistan", function(response) {
      var elements;

      elements = $.html(response);
      console.log(elements);
  });

The only problem is that the elements are not processed HTML. Let's say I want to get a specific divresponse variable - how would I do this? This is done in order to (eventually, after some processing) copy it divto my local page.

Don't worry about cross-domain issues - this is for the Phonegap app

+4
source share
3 answers

$.parseHTML(), DOM, .

:

 $.get("http://en.wikipedia.org/wiki/Afghanistan", function(response) {
    var elements;
    elements = $.parseHTML(response);
    console.log(elements);
    var myDiv = $(elements).find('#idForDiv');
});
+2

, jQuery , . URL- :

$("#mydiv").load( "http://en.wikipedia.org/wiki/Afghanistan #divonpage");

http://jqapi.com/#p=load

+1

If the response from the server is HTML, you can wrap it in a jQuery object and act on it.

I would vouch for this approach:

var server_response = '<div style="background-color:yellow;"><div class="find-me">Hello</div></div>';

$('span').html($(server_response).find('.find-me'));
.find-me {
  color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<span></span>
Run codeHide result
0
source

All Articles