1

Can't seem to get it included. I was using this:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

and then had this:

<script type="text/javascript">

function initialize(){

 $(document).ready(function() {
   $("a").click(function() {
     alert("Hello world!");
   });
 });

}

</script>

....

<body onload="initialize()">
<a href="">Link</a>
</body>

But when I click the link, nothing happens. I have no way of knowing whether jQuery is even including properly. I'm completely new to jQuery, so for all I know I'm making an obvious error here, but it's straight from the jQuery beginner guide, so I'd think it was close at least. Anyone?

1 Answer 1

2

You can just remove that function. It is useless.

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

PS: Your code in work: http://jsfiddle.net/DerekL/h4dmF/

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

11 Comments

Okay, you are at least semi-correct. It worked when I put the jQuery in a separate script (initialize had been doing other things as well, non-jQuery related; they were irrelevant so I do not show them here, but that's why I have it). But now I'd like to understand why this made a difference. Initialize is called onload. So shouldn't that event trigger be set up correctly at onload to wait for a click?
@Aerovistae - Actually, you should not even have this onload="initialize()", and initialize() should be placed inside $(document).
How do I set it up to happen onload if I can't use onload?
When you use $(document).ready (shorthand form: $()), it will automatically run when DOM is loaded. And look closely, there is a () after the initialize function. ((function initialize(){blah})()) and actually, if it is not being called again, you can remove initialize and left with (function(){})().
Exactly. Thanks very much for the extended help.
|

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.