Reputation: 400
I need some help linking my JS file to my html file after I linked jQuery from Google CDN. I could do and put the code inside, but it makes my code look untidy.
This is what I'm doing:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</script>
<script src="images/script.js"></script>
<script src="script.js"></script>
After that, my code does not work. However, when I put it into script tags, it does. I'm linking to script.js correctly, but the code doesn't activate when I try clicking on something. What can I do to fix this?
The code I created was:
$('div').on('click', function () {
$(this).toggleClass('show-description');
});
Thanks!
Upvotes: 0
Views: 71
Reputation: 2837
Try putting your code within document ready function from jQuery. The issue is your DOM is not ready at the time you defined the onclick handler.
jquery document ready function
$( document ).ready(function() {
$('div').on('click', function() {
$(this).toggleClass('show-description');
});
});
Upvotes: 1
Reputation: 46
Check your file pathway for your script.js file in regards to your html file.
Upvotes: 0