0

When loading a html page using JQuery load, I also need to call another function after the html has been loaded in a div. Here is the code i am using.

    $("#div").load("page.html", $('#file1').css('background','black'));

What happens is when the div is re-loaded it also removes the background color even though the code that changes the color is after the load. Any Ideas? Thanks.

2 Answers 2

3

You're missing either an anonymous function or a function reference on the callback (the second argument).

$("#div").load("page.html", function(){
    $('#file1').css('background','black');
});

http://api.jquery.com/load/#callback-function

Sign up to request clarification or add additional context in comments.

1 Comment

The reason for this is that unless you supply something that is a function as the second argument to load(), jQuery will try to send it as data to your url.
1

You can use the callback for this

sample from jquery

$('#result').load('ajax/test.html', function() {
  alert('Load was performed.'); // this executes after the load 
});

Comments

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.