0

I'm trying to use the $.get() function to load a portion of a website into a variable to us later. The code I currently have is:

$(document).ready(function(){
var container;
function loadData(data, targetVar) {
  container = data;
}

$.get('page.html#div', loadData(container));

the problem is the callback function keeps firing before the page loads leaving the variable with an undefined value.

0

2 Answers 2

2

You are executing function with this code:

loadData(container)

This means that you are not passing handler to function, but results of your function. You also need to pass data returned by $.get into your function.

You can put it inside anonymous function:

$.get('page.html#div', function(data) { loadData(data, container); });
Sign up to request clarification or add additional context in comments.

1 Comment

I'm still getting the same result using the anonymous function call
1

If you just want to load part of the remote document, you could use .load method.

$(document).ready(function(){
  $('#container_id').load('page.html #div');
}

1 Comment

Had to reword the page and this was what I went with. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.