Search code examples
jqueryjquery-get

jQuery.get() and returned html data: how to extract piece of codes?


Suppose that you can retrieve an HTML page through a call by using the jQuery.get() function, e.g. as follows:

$.get('xxx.php', 
        function(data) {
        //handle data, that is HTML code....
});

Is there any jQuery solution to extract elements (e.g. div, ul and so on) by the data variable?

PS: I don't believe that using load() to load page fragments is a good idea since I want to minimize the number of server requests.


Solution

  • $.get('xxx.php', 
            function(data) {
            //handle data, that is HTML code....
            var pageDivs = $(data).find('div');
    });
    

    Just make the HTML returned a jquery object and you can use any DOM functions normally.