views:

46

answers:

1

I made a simple div:

<div id="foo"></div>

and I added a click method to it in jquery

$("#foo").click(wow);

function wow(){
  alert("Message");
}

This works fine in Firefox/others, but doesn't work in IE6/IE7? Why? Any specific resources on this?

Edit: The Div already exists in the dom, and the jQuery is run on ready, and it still doesn't work.

+1  A: 

If you're creating the div dynamically, it won't work, and it won't work if that code is run before $(document).ready().

The solutions to both problems, respective, are to use jQuery's live mechanism if you're creating dynamic divs and to place all jQuery event binding in the document ready of the document.

Also make sure that your click handler function is defined before you assign it to the .click method.

Stefan Kendall
It shouldn't work on other browsers either if it's outside ready block or created dynamically.
Tatu Ulmanen
Not entirely true. If it's outside the ready block, there's still a chance that div might be ready or the click method can be assigned. Depends on stuff. Also, if it's depends WHEN the div is created dynamically. If it's happening outside of the doc ready, but before the click is assigned, it could work. I have the OP's code working in both IE6 and IE7 using my pre-requisites. My thoughts are that he's having odd IE timing issues related to the issues presented above.
Stefan Kendall