-1

In Coda 2, I can link to css but when I link to a lot of stuff it doesn't work because nothing happens, especially with jquery. What is wrong with this? Btw here it is so far in jsbin if it matters

main.html

<!DOCTYPE html>
<html>
<head>

<link rel="stylesheet" type="text/css" href="planets.css">
<script src="planets.js"></script>
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>

<title>Planets</title>
</head>

planets.js:

$('#submit').click(function () {
    var checkedPlanet = $("input[name='planets']:checked").val(); 
  $("input:radio").attr("checked", false);

  var age = document.getElementById('age').value;
  var newAge;
  if(checkedPlanet==='mercury'){
    newAge = Math.round(age/0.241);
    alert("You would be"+" "+newAge+" "+ "years old on Mercury!");
  }
  if(checkedPlanet==='venus'){
    newAge = Math.round(age/0.615);
    alert("You would be"+" "+newAge+" "+ "years old on Venus!");
  }
  if(checkedPlanet==='earth'){
    alert('You would be'+" "+ age*1+" " +'on Earth! Duh!');
  }
  if(checkedPlanet==='mars'){
    newAge=Math.round(age/1.88);
    alert("You would be"+" "+newAge+" "+ "years old on Mars!");
  }
  if(checkedPlanet==='jupiter'){}

});
Veronica
  • 39
  • 11
  • Define "it doesn't work." Doesn't work how? Also, the jsbin link you posted has very different code from what you posted here; the number of `link` and `script` tags is different. What doesn't work? – elixenide Jan 31 '14 at 20:59

2 Answers2

1

What Quentin said with a code example:

<script src="//code.jquery.com/jquery-2.0.3.min.js"></script> <!-- Lose http:, and jQuery will load over https too -->
    <script src="planets.js"></script>

    //Wait until the DOM is ready
    $(function(){
        //your code
    });
Francis
  • 1,214
  • 12
  • 19
  • Thanks but this still doesn't work:

    js: //Wait until the DOM is ready $(function(){ //your code $('#submit').click(function () { /*rest of code is here*/ }); });

    – Veronica Jan 31 '14 at 23:14
  • Have you had a look in the browser developer tools (F12), and had a look at what exceptions (if any) are being thrown? – Francis Jan 31 '14 at 23:18
0

You are trying to use jQuery in planets.js and then you are loading jQuery.

Swap your two script elements.


Additionally, you the first thing you try to do in planets.js is bind a click event handler to something with the id submit which doesn't exist in the code you shared.

Presumably it exists further down the document, but it won't have been parsed by the browser when the JS runs.

Move the script to after the element you want to bind to or wrap the code in it in a function that you pass as a DOM ready event handler.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335