Lostsoul Lostsoul - 1 month ago 6
HTML Question

uncaught referenceerror $ is not defined

I'm very new to Javascript(just started a few hours ago and trying to get a script working). I went through a few tutorials on w3 and the 'hello world' code works when I paste it directly into my html but I'm having a problem with a script(I've had problems with other scripts as well but I am not sure what I'm doing wrong).

I have this code that I want to test in my html, I copied the html in and it looks the same then I made a file in my static folder called

edit.js
and copied the javascript into it(exactly as shown). It didn't work no errors on the page but when I click it nothing happens. I tried to paste a w3 hello world code in and that worked but this script does not.

I tried to inspect the code in chrome and that's where I see the above error(under the resources tab). I can open the js file using chrome which makes me think the js file is accessible and pointing correctly but I'm not sure how to get it working. I'm using jinja2 as my template engine to render the html and in my header I have:
<script language="JavaScript" type="text/javascript" src="static/edit.js"></script>

and in my main template(the one that gets rendered on all pages) I have:
<script language="JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>


edit.js(even putting it within the script tag directly on the page I want to use it on doesn't work):

$('#editvalue').click(function(e){$('#storedvalue').hide();$('#altervalue').show();});
$('#savevalue').click(function(e){
var showNew = $('#changevalue').val();
$('#altervalue').hide();
$('#storedvalue').show();
$('#storedvalue span').text(showNew);
});​


html code(its embedded in a larger page):

<head>
<script language="JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript" src="static/edit.js"></script>
</head>
... my html code..
<div id="wrapper">
<div id="container">
<div id="storedvalue"><span>Hello</span> [<a href="javascript:void(0);" id="editvalue">edit</a>]</div>
<div id="altervalue" style="display:none;"><input type="text" name="changevalue" id="changevalue" value="Hello"> [<a href="javascript:void(0);" id="savevalue">save</a>]</div>
</div>
</div>


I have never been able to successfully run a javascript that wasn't on w3 yet. I get the same problem with other scripts even though I see people online saying they work fine for them. Do I need to do anything extra to make this work?

My two questions are, what am I doing wrong? and because Javascript seems to just not work when there's a problem is there a way to get errors or information on what's actually wrong?

I read: Uncaught ReferenceError: $ is not defined? and been trying to figure this out for the last hour and can't see my problem

Answer

First you need to place the jQuery script tag first.

Second, you need to do one of the following things:

  1. Put your code within this function:

    $(document).ready(function(){/*CODE HERE*/});
    
  2. Or like this:

    $(function(){
        /*CODE HERE*/
    });
    

The DOM needs to be ready before you can use it. Placing your code within anonymous functions that are executed on the ready event of the DOM is how you can do this.

Edit:

$(function(){
   $('#editvalue').click(function(e){$('#storedvalue').hide();$('#altervalue').show();});
   $('#savevalue').click(function(e){
     var showNew = $('#changevalue').val();
     $('#altervalue').hide();
     $('#storedvalue').show();
     $('#storedvalue span').text(showNew);
   });​
});