Edwin
Edwin

Reputation: 400

How can I link JQuery AND another JS file to my html?

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

Answers (2)

Liam
Liam

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

Mary Kelly
Mary Kelly

Reputation: 46

Check your file pathway for your script.js file in regards to your html file.

Upvotes: 0

Related Questions