I need insert result from jQuery load to variable.
Content of elemtent with id test on page ajax.html.
$('#result').load('ajax.html #test');
I need insert result from jQuery load to variable.
Content of elemtent with id test on page ajax.html.
$('#result').load('ajax.html #test');
Try
$.ajax
.$.load
is just a shortcut.If you want exactly load func, try this one:
Use
setTimeout
Example on JSFiddle http://jsfiddle.net/kuroir/stD94/
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!