Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
192
rated 0 times [  193] [ 1]  / answers: 1 / hits: 20883  / 12 Years ago, tue, july 10, 2012, 12:00:00

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:



(it's 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?

  • 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 have been trying to figure this out for the last hour and can't see my problem.


More From » jquery

 Answers
17

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);
});​
});

[#84369] Sunday, July 8, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
josuea

Total Points: 609
Total Questions: 121
Total Answers: 104

Location: South Georgia
Member since Fri, Nov 13, 2020
4 Years ago
josuea questions
;