Olokoo
Olokoo

Reputation: 1144

Load function after another function is run in JQuery

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.

Upvotes: 0

Views: 494

Answers (2)

Jared Farrish
Jared Farrish

Reputation: 49188

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

Upvotes: 3

kobe
kobe

Reputation: 15835

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 
});

Upvotes: 1

Related Questions