Reputation: 491
I have a menu item that is animated in the following fiddle by clicking a link, but I actually want the background color fade animation to start once the page has loaded (without the need to click a link). How do I change the code in the following fiddle to animate once the page has loaded?
The code I have currently is:
$("a").click(function(e) {
e.preventDefault();
for (var i = 0; i < 2; i++ ) {
$("#menu-item-9032 a")
.animate( { backgroundColor: "#00afee", color: "#363a47" }, 2000 )
.animate( { backgroundColor: "transparent", color: "#363a47" }, 2000 );
}
});
http://jsfiddle.net/Fe8Jy/500/
If I replace $("a").click(function(e) {
with $(document).ready(function() {
nothing happens, what am I doing wrong?
Upvotes: 0
Views: 36
Reputation: 282895
You must have created a syntax error. Code works fine with jQuery's ready:
$(function() {
for (var i = 0; i < 2; i++ ) {
$("#menu-item-9032 a")
.animate( { backgroundColor: "#00afee", color: "#363a47" }, 2000 )
.animate( { backgroundColor: "transparent", color: "#363a47" }, 2000 );
}
});
Upvotes: 1