Erwin
Erwin

Reputation: 109

jQuery function doesn't work

Why is this not working on my website? It worked on the code snippet function from stackoverflow

It's exactly the same!

<script>
$("#swipe-product").click(function(){
         alert("HELLO");
});
</script>
<div class="text-center" id="swipe-product">
  <img src='http://www.borduurfamke.nl/Embirdtips/Embirdlessen/Studio%20les%20Plaatje.jpg' alt='plaatje'>
</div>

Upvotes: -1

Views: 6688

Answers (5)

Hasti
Hasti

Reputation: 1

I had the same problem. After adding type = "text/javascript" to script code my problem solved. enter image description here

Upvotes: -1

Geeker
Geeker

Reputation: 31

Its working for me. You need to add the reference

<script src="http://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script>

You can check the Demo

Upvotes: 0

Sobiaholic
Sobiaholic

Reputation: 2967

  1. You haven't included jQuery library, if yes, double check the path.

  2. Script runs before jQuery is completely loaded, use $( document ).ready()

    function like below, or put the script in the footer.

$( document ).ready() Reference

<script>
$(document).ready(function(){
   $("#swipe-product").click(function(){
           alert("HELLO");
   });
})
</script>

Upvotes: 0

Pixelomo
Pixelomo

Reputation: 6737

You need a local copy of jQuery or a link to a cdn version of it. The current link you're including is to a relative path within your project so it's looking for a copy of jQuery within your js folder.

You can download jQuery here and place it in that folder, but it might be easier just while you're getting used to it to use a cdn, this means you use a copy of jQuery hosted by someone like Google. It's not always the fastest or most stable method but it's definitely the easiest.

Replace

<script src="/js/jquery-1.11.2.min.js"></script>

With

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

Upvotes: 0

Quentin
Quentin

Reputation: 944455

Most likely causes:

  • You haven't included the jQuery library
  • You have placed the script element before the div element, so that when it runs, the div doesn't exist, so there isn't an element to bind to. Move the script to later in the document or learn about ready().

Upvotes: 1

Related Questions