Code:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
$("#clicker").click(function () {
alert("Hello!");
$(".hide_div").hide();
});
</script>
The above code doesn't work. When I click on #clicker, it doesn't alert and and it doesn't hide. I checked the console and I get no errors. I also checked to see if JQuery was loading and indeed it is. So not sure what the issue is. I also did a document ready function with an alert and that worked so not sure what I am doing wrong. Please help. Thanks!
You have to wrap your Javascript-Code with
$(document).ready(function(){});
Look this JSfiddle.JS Code:
I found the best solution for this problem by using ON with $(document).
for id start with see below:
finally after 1 week I not need to add onclick triger. I hope this will help many people
You can use
$(function(){ // code });
which is executed when the document is ready to execute the code inside that block.Try adding
$(document).ready(function(){
to the beginning of your script, and then});
. Also, does thediv
have the id in it properly, i.e., as an id, not a class, etc.?Just a quick check, if you are using client-side templating engine such as handlebars, your js will load after document.ready, hence there will be no element to bind the event to, therefore either use onclick handler or use it on the body and check for current target
Your code may work without document.ready() just be sure that your script is after the #clicker. Checkout this demo: http://jsbin.com/aPAsaZo/1/
The idea in the ready concept. If you sure that your script is the latest thing in your page or it is after the affected element, it will work.