Hogen
Hogen

Reputation: 3

jQuery simple function wont work

Why wont an alert box pop up when happen when I 'Click me'?

Im a total noob at javascript/jquery but im trying to learn a bit I just found this example in a tutorial but it wont work

Look: http://removed

edit: removed my site link since question is solved

Upvotes: 0

Views: 174

Answers (2)

Agos
Agos

Reputation: 19460

You also need to put your code inside $(document).ready, like this:

$(document).ready(function() {
    $("a").click(function(){
        alert("Hi");
        return false;
    })
});

Otherwise the code won't be properly bound to the event, since it will try to bind it when the page still hasn't loaded.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039588

You need to return false from the click handler or the page might get redirected:

$("a").click(function() {
    alert("Hello world!");
    return false;
});

Also don't forget to include jQuery in the head section:

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

Upvotes: 2

Related Questions