21

I need insert result from jQuery load to variable.

Content of elemtent with id test on page ajax.html.

$('#result').load('ajax.html #test');
Alex H
  • 1,424
  • 1
  • 11
  • 25
honzahommer
  • 839
  • 3
  • 10
  • 15

9 Answers9

28

Try using jQuery.get instead.

Eg.

$.get('ajax.html', function (data) {
    data = $(data).find('#test');
    // data contains your html
});
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Daniel Baulig
  • 10,739
  • 6
  • 44
  • 43
11

More or less doing the same as above but simpler:

var dt;
    $.get("ajax.html", function (data) {
        dt = data;
        // data contains your html
    });
Tsonev
  • 435
  • 7
  • 17
  • 1
    This answer seems to me the best one. Plese look also at this gist: https://gist.github.com/geilt/817ff32b20414ddd5a16 – Redips77 Nov 18 '19 at 11:38
11
$('#result').load('ajax.html #test', function(result) {
    var variable = $('#result').html();
});
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • That looks like a reasonable methodology, but that's setting a variable in the scope of only the callback.. which wouldn't be terribly helpful. – Jamie Wong Jun 01 '11 at 15:19
  • 2
    @Jamie Wong, as we all know the result of an AJAX call is available only in the callback, so setting the value of this variable anywhere outside of this success callback would simply be meaningless. – Darin Dimitrov Jun 01 '11 at 15:21
  • 1
    @Jamie: So, set a variable in the global scope, or whatever. – gen_Eric Jun 01 '11 at 15:21
  • @Darin Your approach looks hackish to me. It's less code than using a normal AJAX request and searching for the `#test` content yourself, but it does an unnecessary load into `#result`. – Alin Purcaru Jun 01 '11 at 15:22
  • 3
    @Alin Purcaru, why unnecessary? I thought the OP's intention was to load the result of the AJAX call into `#result` and in addition to this assign it to a variable. If this is not the case obviously using `$.ajax()` would be better. – Darin Dimitrov Jun 01 '11 at 15:27
  • @Darin I guest this can only be answered by him. We both have different *assumptions*. – Alin Purcaru Jun 01 '11 at 15:47
7
$(document).ready(function(){
    $('#result').load('/ p#name', function(result) {
        var obj = $(this).find('p#name'), html = obj.html();
        obj.css({'font-size':40});
        $(this).append($('<div>').text(html));
    });
});

Example on JSFiddle http://jsfiddle.net/kuroir/stD94/

MarioRicalde
  • 9,131
  • 6
  • 40
  • 42
5

I'm posting this message on all the stackoverflow threads that ask for help in cramming data from a jQuery ajax method into a variable, rather than into an HTML element, because I had a tough time finding a solution that would work. I finally found a solution, and I just want to share with anyone who is having a hard time with this.

My problem was that I was really struggling with getting the results of jQuery ajax into my variables at the "document.ready" stage of events.

My tests showed me that jQuery's ajax would load into my variables when a user triggered an "onchange" event of a select box after the page had already loaded, but the data would not feed the variables when the page first loaded. But I didn't want to trigger any ajax on my "onchange" event; I wanted the variables, fed with ajax data on page load, to handle the work. The variables are used to change data on the fly when the user triggers an "onchange" event. This way there is no lag/ajax while the user is interacting with the form; the data is available once the page loads.

I tried many, many, many different methods, but in the end, the code I needed was on this stackoverflow thread : JQuery - Storing ajax response into global variable

I used Charles Guilbert's response. Using his answer, I am able to get data into my variables, even when my page first loads.

Here's an example of the working script - hope it helps someone!

jQuery.extend
(
    {
        getValues: function(url) 
        {
            var result = null;
            $.ajax(
                {
                    url: url,
                    type: 'get',
                    dataType: 'html',
                    async: false,
                    cache: false,
                    success: function(data) 
                    {
                        result = data;
                    }
                }
            );
           return result;
        }
    }
);

// Option List 1, when "Cats" is selected elsewhere
optList1_Cats += $.getValues("/MyData.aspx?iListNum=1&sVal=cats");

// Option List 1, when "Dogs" is selected elsewhere
optList1_Dogs += $.getValues("/MyData.aspx?iListNum=1&sVal=dogs");

// Option List 2, when "Cats" is selected elsewhere
optList2_Cats += $.getValues("/MyData.aspx?iListNum=2&sVal=cats");

// Option List 2, when "Dogs" is selected elsewhere
optList2_Dogs += $.getValues("/MyData.aspx?iListNum=2&sVal=dogs");
Community
  • 1
  • 1
CityPickle
  • 131
  • 1
  • 1
3

Try $.ajax. $.load is just a shortcut.

Alin Purcaru
  • 43,655
  • 12
  • 77
  • 90
2

If you want exactly load func, try this one:

var temp;
$(new Object()).load('YourUrl', { param: "smth" }, function(data){
  $('#element').prepend(data); //if you want prepend/append result to some html
  temp = data; //if you want put to variable
});
Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
1
$('#submit').click(function(){ var taval = $("#message").val();
$("#showcomment").load('comments.php', {commentval:taval}) });
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
danish
  • 29
  • 4
0

Use setTimeout

<script> 
    my_var=$('#hidden').load('new.html');
    setTimeout(function(){
        my_var=my_var.html();
        alert(my_var);
    }, 500);
</script>
Vitalicus
  • 1,188
  • 13
  • 15