I receive page ajax - request

$.ajax({ url: 'sortmonth', success: function(data){ } }); 

How to get data from a specific div? For example, the data passed to:

 <!DOCTYPE html> <html lang="ru-RU"> <head> <meta charset="UTF-8"> </head> <body> <div id = 'container'> </div> </body> 

How to get data from div 'container'?

  • directly from data without changing the html page? - Horchynskyi
  • document.getElementById ('container'). innerHtml - Drag13
  • one
    @Vitalii of this element will not be on the page, the html-code is received via AJAX - andreymal

4 answers 4

Well, let's do ...

 var dynamicPage = `<!DOCTYPE html> <html lang="ru-RU"> <head> <meta charset="UTF-8"> </head> <body> <div id="container"> DATA </div> </body>`; var parser = new DOMParser(); var doc = parser.parseFromString(dynamicPage,"text/html"); var val = doc.getElementById('container').innerHTML; console.log(val); 

Pros: Do not use third-party bibliotek. We receive object with which it is possible to work as with the normal document. Cons: Formally, this technology is experimental. But it is supported by all browsers with IE 10. IE9 does not support.

    You can use the .load() method

    The .load() method allows you to specify a portion of a deleted document to insert. This is available using special syntax in the url parameter. If one or more whitespace characters are included in the string, then the first word, after the first whitespace, will assume the jQuery selector, which determines which part will be inserted.

    If you need to insert the resulting HTML code into the page:

     $('селектор, куда нужно вставить').load('sortmonth #container') 

    If you need to get exactly the HTML code:

     var $temp = $('<div>').load('sortmonth #container', function () { console.log($temp.html()) // $temp.html() HTML-код контейнера #container }) 

      On jQuery like this:

       var myDivData = $(data).find('div#container').html(); 
      • 2
        This element will not be on the page, the html-code is received via AJAX - andreymal

      Do as follows:

       var html = $('<div />').append(data); var container = data.find('#container'); console.log(container);