Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery listener click not working

So I have this code that should listen for a click on #button but it won't work, and is driving me crazy!

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
    $('#button').click(function() {
        alert('OK!');
    });
</script>

and the HTML:

<input id="button" type="button" value="OK" />

This is strange. Any help is welcome!

like image 694
Axel Latvala Avatar asked Sep 24 '11 17:09

Axel Latvala


People also ask

Why click function is not working in jQuery?

jQuery click not working at the time page loading, jQuery Onclick Method is tried to an element or selector. As a result, the binding will fail if the element we wish to click isn't present when the page is ready.

Why is my event listener not working?

If your event listener not working is dependent on some logic, whether it's about which element it'll listen on or if it's registered at all, the first step is to check that the listener is indeed added to the element. Using a breakpoint in the developer tools , a logpoint or console.

What is click event listener?

The JavaScript addEventListener() method allows you to set up functions to be called when a specified event happens, such as when a user clicks a button.

Is clicked function in jQuery?

To trigger the onclick function in jQuery, click() method is used. For example, on clicking a paragraph on a document, a click event will be triggered by the $(“p”). click() method. The user can attach a function to a click method whenever an event of a click occurs to run the function.


2 Answers

Write your code inside document.ready function

<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js">
</script> 
<script type="text/javascript">
$(document).ready(function() {
    $('#button').click(function() {
        alert('OK!');
    });
});
</script>


 OR
<script type="text/javascript">
    function on_click() {
          alert("OK !!");
    }
    $(document).ready(function() { 
         $("#button").click(on_click);
    });
</script>

hope you get some idea from this

like image 69
Ujjwal Manandhar Avatar answered Oct 26 '22 08:10

Ujjwal Manandhar


Here's what the code should look like:

<script type="text/javascript"
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js">
</script> 
<script type="text/javascript"> 
    // Now that jquery is include, place the code to wire up the button here
    $(function(){
        // Once the document.onload event fires, attach the click
        // event handler for the button
        $('#button').click(function() { 
            alert('OK!'); 
        });
    });
</script> 
<input id="button" type="button" value="OK" /> 

Here's the jquery documentation that relates to this: http://docs.jquery.com/How_jQuery_Works

like image 45
Chris Pietschmann Avatar answered Oct 26 '22 08:10

Chris Pietschmann