1

I'm new to jQuery, so this should be a simple question.

As far as I understand, I can bind a method to listen to an event, such as the click of a button, using

$('#buttonID').bind('click', function (){//some code});

However, this isn't working for me, so I must be doing something wrong.

This is my simple html file:

        <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js'></script>
    <script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js'></script>
    <script src='test.js'></script>
    </head>

    <body>
    <input id="SignIn" type="button" value="Sign In"></input>
    </body>
    </html>

Apart from loading jQuery files, it loads file test.js, which looks like this:

// JavaScript Document
$('#SignIn').bind('click', function() {alert('hi');});

Is that not enough for binding? I was hoping this would fire an alert dialog, but it doesn't, it seems the callback is not executed at all.

What is wrong here? Both files (html and js) are located in the same directory, and Google Chrome does not complain about anything in the JavaScript console, so from that end, everything should be fine.

Thanks for all help!

4
  • 3
    You need a $(document).ready function, read the documentation for jQuery, it's in almost every example, an is the first thing you'll learn. Commented Sep 5, 2014 at 11:37
  • 2
    Also, use on, not bind Commented Sep 5, 2014 at 11:38
  • @adeneo he is using jQuery 1.3.2 mate, Commented Sep 5, 2014 at 11:39
  • @BarlasApaydin - And that sound OK to you ? Then he should update! Commented Sep 5, 2014 at 12:23

8 Answers 8

3

Wrap your code in document ready handler. It accepts a function which executes when DOM is fully loaded.

As you are using jQuery 1.3

$(document).ready(function() {
    $('#SignIn').bind('click', function() {
        alert('hi');
    });
});

For jQuery 1.7+,

$(document).ready(function() {
    $('#SignIn').on('click', function() {
        alert('hi');
    });
});

Additionally, As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah yes, quick and simple answer. Thanks! Will mark as answer once SO allows me to do so.
1
$(function(){
    $('#SignIn').bind('click', function() {alert('hi');});
})

Comments

1

Try

$(function(){
   $('#SignIn').click(function() {alert('hi');});
});

Comments

1

Move your Javascript to the bottom of the HTML page, right above the closing body tag.

That way the DOM is ready when it is loaded, and there's no need for $(document).ready() calls.

https://developer.yahoo.com/performance/rules.html#js_bottom

Comments

1

You may want to include your JavaScript files at the very bottom, and everything should work as expected. It is recommended to do this and to include CSS files at the top (head tag). For more information see link included by @Grim...

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<input id="SignIn" type="button" value="Sign In"></input>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js'></script>
<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js'></script>
<script src='test.js'></script>
</body>
</html>

Comments

0
$( "#target" ).click(function() {

//Write some code here

});

Comments

0

You can try this.

$(document).ready(function(){
  $('#SignIn').on('click', function() {alert('hi');});

});

1 Comment

Please explain what is wrong with OP's code and why this solves the problem by editing your answer.
0

You can use this.

  • $(document).ready(function () { $("#SignIn").click(function () { alert('demo'); }); });

1 Comment

#user2606742 - If answer is useful, Please click on 'correct' and give 'upvote' so it will helps other to find out correct solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.