-5

i want to show alert when user clicking on link "Go"... but the function does not run... .....help me please..... here is my code:

<!DOCTYPE html>
 <html>
 <head>
    <script src="js/jquery.autocomplete.js"></script>
    <script src="js/trip.min.js"></script>
    <script src="js/chosen.jquery.min.js"></script>
    <script>
     $('.clk-btn').click(function(e){
       e.preventDefault();
       alert("Error");
    });
    </script>
    </head>
    <body>
     <a class="clk-btn"  href="#"  >Go</a>
    </body>
    </html>
molenzwiebel
  • 740
  • 4
  • 20

1 Answers1

4

You should make use of document ready:

$(function(){
    $('.clk-btn').click(function(e){
        e.preventDefault();
        alert("Error");
    });
});

Otherwise the click handler will not be bound to your anchor.

$(function(){
     $('.clk-btn').click(function(e){
       e.preventDefault();
       alert("Error");
    });
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="clk-btn"  href="#"  >Go</a>
Christos
  • 50,311
  • 8
  • 62
  • 97