CHawk
CHawk

Reputation: 1356

jQuery - on() not working (Simple Example)

I'm having some trouble with my Jquery code.

<html>
<head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
        $(function() {
            $(".upvote").on("click", function() 
            {
                alert('test');
            }
        }); 
    </script>
</head>
<body style="background-color:black; color:white;">
    <form action="#" method="post">
        <input type="submit" class="upvote" value=" + " />
    </form>
</body>
</html>

When I click the button, nothing happens. I checked and made sure I have jQuery 1.7. Can anyone help?

Upvotes: 8

Views: 7501

Answers (4)

Jayendra
Jayendra

Reputation: 52769

Should be

$(".upvote").on("click", function() {
  alert('test');
});

Upvotes: 2

Jasper
Jasper

Reputation: 75993

You have an error in your code, otherwise it works find:

Change

        $(".upvote").on("click", function() 
        {
            alert('test');
        }

To:

        $(".upvote").on("click", function() 
        {
            alert('test');
        });//notice I had to add the `);` which closes the on function properly

Here is a jsfiddle of your code working with the small tweak: http://jsfiddle.net/jasper/AVusC/

Upvotes: 1

Exception
Exception

Reputation: 8379

It works fine if you give

 $(".upvote").on("click", function() 
        {
            alert('test');
        });

instead of

 $(".upvote").on("click", function() 
        {
            alert('test');
        }

Upvotes: 1

JaredPar
JaredPar

Reputation: 754545

You are missing a closing ); on your handler. It's causing a syntax error which results in the code not running

$(".upvote").on("click", function() {
  alert('test');
});

Upvotes: 13

Related Questions