tags:

views:

158

answers:

3

I am trying to do a simple jQuery tutorial, but it won't work.

<html>                                                                  
 <head>                    
    <title></title>
    <script type="text/javascript" src="jquery-1.3.2.js">                                         
     $(function() {
      $('a').click(function() {
   $('#box').fadeOut();
  });
 });                                    
</script>           
<style type="text/css">
     #box
 {  background: red;
  width: 300px;
  height: 300px;
 }
</style>

</head>                                                                 
<body>                                
    <div id="box" ></div>
     <a href="#">Click Me</a>

</body>                                                                    
</html>

I have copied and pasted the jquery filename: jquery-1.3.2

I can't see what is wrong? I am using Firefox.

+9  A: 

First, make sure you have the jquery-1.3.2.js in your directory.

Then, change your script tags:

<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
            $('a').click(function() {
                    $('#box').fadeOut();
            });
    });
</script>
Yuval A
+15  A: 

Put your click event code in different script block.

    <script type="text/javascript" src="jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(function() {
            $('a').click(function() {
                    $('#box').fadeOut();
            });
    }); 
    </script>

The error you committed remembers me of Degrading Script Tags

Script tags that reference external resources (via the src attribute) are no longer able to execute script embedded within the tag itself.

Eddy
+3  A: 

You need to be so careful with where you put the different parts of the code for it to work.

Have a look at this code which worked for me.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
<html>
<head>
  <title>Fade Out Red Box</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <style type="text/css">
  #box
  {
    background: red;
    width: 300px;
    height: 300px;
  }
  </style>
</head>

<body>
  <div id="box"></div>
  <a href="#">Click me</a>

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

  <script type="text/javascript">
    $(document).ready(function(){
      $("a").click(function() {
        $("#box").fadeOut("slow");
      });
    });
  </script>
</body>
</html>
Ian Roke
hmmm that is interesting, you have the script tags in the body of the doc. Thanks it works.
Ankur
Where you put your SCRIPT tags is up to you. In my code you can put them in the HEAD and it will still work. It just dictates when you start seeing your page. Everything in the HEAD is loaded before the page starts to draw. In your code you also tried to link to an external file in the SCRIPT tag *and* provide some code which isn't possible.
Ian Roke