jquery .bind() and/or .ready() not working

So I have this code:

var bindAll;
bindAll = function ()
    $('#somediv').bind('mouseover', function(){do something});

var init;
init = function ()


and it does not work. But if I put the bind on a timer by replacing:



tt = setTimeout('bindAll()', 1000);

It suddenly works perfectly. What!??



You aren't passing init to $(document).ready, you're passing whatever init returns.

Try this:



When you were trying to pass the function, you were actually running it. At the time of you running it, the DOM wasn't ready, so the bindings to the element didn't take place, because it didn't exist.

When you did the timeout, it worked because it took less then one second for the DOM to finish, which means by the time it gets run, the element is there, so it can bind the event.


jQuery ready expects a handler, not a function call.

// $(document).ready(init()); <-- Not working
$(document).ready(init); <-- Working!

Working example at: http://jsfiddle.net/marcosfromero/Qwghb/


just pass init without parenthesis into the ready function. it is being executed immediately in your case and not on document ready:



Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.