Arif YILMAZ
Arif YILMAZ

Reputation: 5866

Sometimes jQuery plugin is not loaded

I am using jQuery a lot, but sometimes I get stuck because my browser cannot see the jQuery library, or maybe loads the library after running the my JavaScript code.

Pseudo-code to explain my question:

<html>
    <head>
        I always load CSS here
        I always load jquery here
    </head>
    <body>
      <p class="link-style3"><span id="my_tag"><span>Bize Hemen Yazın</span></span></p>
      <script>
        $(function() {
          $('#my_tag').click(function(e) { 
            alert('tesrt');
          });
        });
      </script>
    </body>
  </html>

I always put my stuff in the order like below, but this doesn't work now. When I click the <span id="my_tag">, it doesn't do anything, and doesn't return any error.

what should I do?

Here is the entire code jsfiddle

Upvotes: 1

Views: 1552

Answers (2)

Gregone
Gregone

Reputation: 447

A few things you can do:

  • inspect your document (network pane in devtools) to see if everything is loading correctly
  • Move your scripts to the bottom of the page
  • use $(document).ready(function(){ ... });

Upvotes: 1

GautamD31
GautamD31

Reputation: 28763

Try to avoid some syntax errors like(suggestable only)

<script type="text/javascript">
 $(function() {
    $('#my_tag').click(function() { 
        alert('tesrt');
     });
  })
</script>

and put your code at the top after you load the js files

Upvotes: 1

Related Questions