How to parse the returned html page using jQuery.get ()

Until recently, I used $.load() to get a specific part of another page and load it into the current one, but when the user initiates these requests, they can queue, and I found that I need a way to interrupt them so that I don’t click - happy users violate my page.

This led me to $.get() , which works fine and allows me to cancel the request if another starts before the first return, but now I need to parse the returned html (this is a whole page with elements like doc and head) and get only one div from him.

How do I achieve this?

+6
source share
3 answers

This should work:

 $.get('ajax/test.html', function(data) { var my_div = $('#my_div', $(data)); }); 
+9
source

In the callback:

 function(data) { // we wrap data with jQuery here: $(data).find("#id"); } 
+8
source

I tried a twofold example, but it returned an object :: object, so it changed a bit:

 $.get($(this).attr("href"), function(returnedHTML) { alert($('#content', $(returnedHTML)).html()); }) 
0
source

Source: https://habr.com/ru/post/896773/


All Articles