Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery click function not getting called

Tags:

jquery

Why is the click function not called?

    <html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js" type="text/javascript" />
    <script type="text/javascript">
        $('#test').click(function () {
            alert('clicked');            
        });    
    </script>

</head>
<body>
    <a id="test" href="#">Click here</a>
</body>
</html>
like image 992
Sampat Avatar asked Mar 20 '26 16:03

Sampat


2 Answers

You are adding an event to an element that doesn't exist yet. Wrap your event listener in a DOM-ready function or move your script tag below the element.

like image 149
Mauvis Ledford Avatar answered Mar 23 '26 07:03

Mauvis Ledford


you must have a closing script tag at:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js" 
type="text/javascript" >
</script>

and you must call your click binding in:

$(document).ready(function(){
  $('#test').click(function () {
        alert('clicked');            
    }); 
});
like image 27
Headshota Avatar answered Mar 23 '26 06:03

Headshota



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!