0

How can I save the return of the JQuery AJAX load() function in a variable? For example var temp="";

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

So instead of saving the return in div1, I want to save it in the variable temp.

Thank you

megawac
  • 10,953
  • 5
  • 40
  • 61
RAM Diego
  • 69
  • 4

3 Answers3

0

load() is just a shortcut for $.get that automagically inserts the content as well. You can use $.get instead if you want to store the data in a variable instead of directly inserting to an element

$.get('demo_test.txt', function(data) {
    var temp = $('<div />', {html : data}).find('#p1');
});
adeneo
  • 312,895
  • 29
  • 395
  • 388
0

You could try something like:

$( "#div1" ).load( "demo_test.txt", function(data) {
  var temp = data;
});
cgf
  • 3,369
  • 7
  • 45
  • 65
0
$( "#div1" ).load( "demo_test.txt", function( response, status, xhr ) {
     var saved_response = response;
});

Look more at https://api.jquery.com/load/

Miljan Puzović
  • 5,840
  • 1
  • 24
  • 30