Juri Juri - 14 days ago 7
jQuery Question

Traversing the DOM with load() method

In jQuery, the

.load()
method allows you to import the specific content, for example, of a certain
id
using selectors:

$("#div1").load("demo_test.txt #p1");


With jQuery (and if possible without functions but just using selectors), how do I select, for example, the previous element to that
id
?

Eg 1 (mistaken):

$("#div1").load("demo_test.txt #p1").prev();


Eg 2 (mistaken):

$("#div1").load("demo_test.txt #p1.prev()");

Answer

You can't do that with load() method instead of that use jQuery.get and get the element from the response.

$.get("demo_test.txt",function(res){ 
  $("#div1").html($(res).find('#p').prev()); 
},'html')