How can I use jQuery to interact with a specific div, but not in the current document - in a variable containing HTML?

How can I use jQuery for messing with a specific div, but not in the current document - in a variable that contains HTML?

The fact is that I want to show a preview of the page (part of its contents) in a modal window when a link to this page is clicked. Well, onClick I load all this HTML into a variable via JSON, and then ... how would I find the specific div I need? It will be almost impossible to parse it with PHP before converting it to JSON and returning the jQuery processor due to the deep hierarchy. Basically, you can even make smth like $ ('div # some-id'). Blabla (); not for the current document, but for a document stored in a variable?

Thank you all in advance.

+3
source share
4 answers

Use the optional context argument for jquery calls. In other words,

$('div#some-id', document).blabla();

+4
source

You can also do it like this (which is similar to what @nsayer wrote:

$.get(url, {parameters}, function(data){
    $('#somePartOfThePage', data);
});
0
source

jQuery $.ajax, - ...

$.ajax({
  success: function(responseText) {
    $("#previewDiv").html($("div#content", responseText).html());
  }
});

"success" html ( , $.ajax dataType), "responseText" ( ), , html div #content div .

, ?

0

jQuery . load() :

$('#modalWindow').load('http://example.com/ #content');
-1

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


All Articles