views:

146

answers:

4

I'm a complete newbie as far as jQuery is concerned. I also don't know much of JavaScript except for some very basic stuff.

I'm following this tutorial here: http://docs.jquery.com/How_jQuery_Works

And nothing's working! :-)

I created a folder on my machine's hard drive here: C:\rnd\jQuery

Then, in that folder, I put the jquery.x.x.js file that I downloaded from the jQuery website and I created a test.html file and wrote the following code in it:

<!doctype html>
<html>
  <head>
    <script type="text/javascript" src="jquery-1.4.2.js"></script>

    <style type="text/css">
      a.test { font-weight: bold; }
    </style>

    <script type="text/javascript">
      $.(document).ready(function() {
        $("a").addClass("test");
        $("a").click(function(event) {
          alert("Thanks for visiting.");
          event.preventDefault();
        });
      });
    </script>
  </head>
  <body>
    <a href="http://jquery.com/"&gt;jQuery&lt;/a&gt;
  </body>
</html>

It just does the normal behavior of taking me to the jQuery website. I ran it on Chrome, IE and Firefox. I have JavaScript enabled in the browsers. It's all the same everywhere. It doesn't do anything that I expected it to do. It just takes me to the website.

Except on IE, it shows me that message box saying an error occurred in your script. When I click "Yes" to debug, it opens up the debugger but it doesn't highlight any line of code so I don't really know what's happening.

Then, when I had the following line to my code:

$("a").hide("slow");

And my complete code looks like this:

<!doctype html>
<html>
  <head>
    <script type="text/javascript" src="jquery-1.4.2.js"></script>

    <style type="text/css">
      a.test { font-weight: bold; }
    </style>

    <script type="text/javascript">
      $.(document).ready(function() {
        $("a").addClass("test");
        $("a").click(function(event) {
          alert("Thanks for visiting.");
          event.preventDefault();
          $("a").hide("slow");
        });
      });
    </script>
  </head>
  <body>
    <a href="http://jquery.com/"&gt;jQuery&lt;/a&gt;
  </body>
</html>

At this, nothing different happens in Firefox and Chrome, but in IE, it breaks again into the debugger, this time with this line highlighted in yellow, and it reports that the identifier jQuery is not defined.

(function($) {
  $.fn.textDropShadow = function(){
    $(this).html('<span class="jq-shadow">'+$(this).html()+'</span><span>'+$(this).html()+'</span>');
    return $(this);
  }
})(jQuery);

And that, I believe is some JavaScript code on the jQuery website.

I feel completely lost. Any help would be appreciated.

Update: I have my complete HTML and it is valid XHTML. It's too bad the browser displays that as an HTML response stream and I can't even get it to show up here as a script. Damn! How do you make HTML show up here?

+1  A: 

Did you include the line:

<script type="text/javascript" src="jquery.js"></script>

You need to show more of your page for us to know what's wrong.

tster
yes, he did. His code just needed properly formatting.
Andy E
Yes, I did. I'd formatted my script as code as well but it doesn't show up. I tried enclosing everything in a <pre> tag as well but it didn't show up even then. Sad.
Water Cooler v2
@Water Cooler v2: You just have to indent your code by four spaces and it will be formatted as such. No special HTML tags needed.
Felix Kling
@Andy E: How did you know I'd done that? Wow! I know I'm sounding dumb but I guess that's how we all do when we're newbies.
Water Cooler v2
@Andy E: Oh, I see. It's right up there, it shows the script inclusion. Sorry. :-)
Water Cooler v2
@Water: Users with 2k rep here can edit your post and see the source. David Dorward posted the same answer as this one, then shortly afterwards deleted it with a comment saying *"Oh, you have..."*.
Andy E
+15  A: 

I can see one issue. You have a . following the $ in the document ready statement.

$.(document).ready(function()
 ^--- remove the .!

It should look like this:

$(document).ready(function() 
Andy E
Yay! You're the man. Thanks a bunch.
Water Cooler v2
"You're the man, and I accepted your answer" would sound better. `;-)`
kiamlaluno
I did want to, but I had to wait a couple of minutes before SO would let me accept any answer. :-)
Water Cooler v2
+4  A: 

First off make sure you have the jQuery library referenced before writing any jQuery code (or loading any plugins)

<script type="text/javascript" src="{path to jquery.x.x.js}" ></script>

Also, it should be $(document).ready(function() { });

not $.(document)

Mutt
+2  A: 

Obviously the problem was an extra dot in the $.document part however here is a tip for you when learning jquery.

You may find yourself in the situation that you have another javascript library running on the page and it's using the $ symbol too. A good way to keep your jquery separate from the other library but still share the $ symbol is to alias $ inside your jquery init statement.. like so.

// the dollar symbol doesn't exist outside of this as we started it with jQuery so i personally like this approach.

jQuery(document).ready(function($) {
    $('#...');
});
Paul Dragoonis
@Paul Dragoonis: Thanks, Paul. Great tip. I also learnt from the API a short while ago that I could use the .noConflict() function to tell the JavaScript interpreter that I will use the name jQuery instead of the $ sign as it might have been overloaded by some native JavaScript code. Thanks for letting me know this.
Water Cooler v2
Yes basically the same thing! You're on the right track, noConflict() might be preferred because it's self-documenting meaning that the function name implies what you're attempting to do.
Paul Dragoonis