2

I've got this statement:

$('#queueCount').load("http://localhost:8081/smsc/userRole/getQueueCount/");

#queueCount is a <div> id. How can I store loaded value in variable? For example, like this:

var value=$('#queueCount').load("http://localhost:8081/smsc/userRole/getQueueCount/");
Tony
  • 3,605
  • 14
  • 52
  • 84
  • I bet everything you need to know is here http://api.jquery.com/load/ – andrex Sep 22 '14 at 08:58
  • possible duplicate of [How to return the response from an Ajax call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Qantas 94 Heavy Sep 22 '14 at 09:04

2 Answers2

1

Three ways.. (1st and 2nd preffered)

var vardata;
$.get('http://localhost:8081/smsc/userRole/getQueueCount/', function(data) {
    vardata = data;
});

or

var vardata;
$.ajax({
    url: 'http://localhost:8081/smsc/userRole/getQueueCount/',
    success: function(data) {
        vardata = data;
    }
});

or

var vardata;
$('#queueCount').load("http://localhost:8081/smsc/userRole/getQueueCount/");
vardata = $('#queueCount').html();
void
  • 36,090
  • 8
  • 62
  • 107
  • 2
    #2 and #3 won't work properly. 2 won't add the data to the element, #3 won't have loaded in time. – Scimonster Sep 22 '14 at 09:02
  • yeah, in the 3rd one if i will chain them it will work i guess.. var vardata=$('#queueCount').load("http://localhost:8081/smsc/userRole/getQueueCount/").html(); – void Sep 22 '14 at 09:04
0

http://jsfiddle.net/hjggsqx2/2/

This is how I'd do it. I also like the syntax [website] [classname/id name]. It is more convenient.

var $div = $('<div>');
alert($div);

$div.load('http://www.aligajani.com .container', function(){
    // now $(this) contains .container
});
Ali Gajani
  • 14,762
  • 12
  • 59
  • 100