javascript - jQuery class selector and click() -
i'm trying alert happen when clicked. can working in jsfiddle, not in production code:
jsfiddle example works (jquery 1.5 loaded)
html (in case jsfiddle inaccessible):
<!doctype html><html><head><title>test</title></head> <body> <h1>25 feb 2011</h1><h3>abc</h3><ul> <li class="todoitem">test—5 minutes</li> </ul> </body></html>
javascript:
$(".todoitem").click(function() { alert('item selected'); });
non-working production example:
<!doctype html><html><head><title>test</title> <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.5.1.min.js" type="text/javascript"></script> <script type="text/javascript"> $(".todoitem").click(function() { alert('item selected'); }); </script> </head> <body> <h1>25 feb 2011</h1><h3>abc</h3><ul><li class="todoitem">test—5 minutes</li></ul> </body> </html>
safari's inspector indicate jquery being loaded correctly, that's not issue. far can tell, these 2 pieces of code identical, latter isn't working. can see i've done wrong?
you need wrap code in $(document).ready()
this work
$(document).ready(function(){ $(".todoitem").click(function() { alert('item selected'); }); });
jsfiddle automatically
Comments
Post a Comment