0

The below javascript code is displaying nothing,although this code works by Fiddle perfectly. The javascript console gives me 2 error messages which I don't understand: Failed to load resource: net::ERR_FILE_NOT_FOUND and Uncaught TypeError: Cannot set property 'onclick' of null, so anyone understands the reason of the error messages and how can I get my code worked so it will calculate & display stuff that users will choose?

The code is:

var numericalValues = new Array();
numericalValues["Alot"]= 4;
numericalValues["NotMuch"]= 2;
numericalValues["NoSometimes"]= 3;
numericalValues["Hate"]= 0;
numericalValues["Chocolate"]= 4;
numericalValues["Carrot"]= 0;
numericalValues["Both"]= 2;
numericalValues["None"]= 0;


function getScoreChoco()
{
var scoreChoco = 0;
var form = document.forms["form"];
var choco = form.elements["choco"];
for(var i=0; i<choco.length; i++)
{
    if(choco[i].checked)
    {
    scoreChoco = numericalValues[choco[i].value];
    break;
    }

}
return scoreChoco;
}

function getScoreCake()
{
var scoreCake = 0;
var form = document.forms["form"];
var cake = form.elements["cake"];

for(var i=0; i<cake.length; i++)
{
  if(cake[i].checked)
  {
  scoreCake = numericalValues[cake[i].value];
  break;
  }

}
return scoreCake;
}


function getTotal()
{

var totalScore = getScoreCake() + getScoreChoco();


document.getElementById('result').innerHTML = "Your total score is: "+totalScore;



document.getElementById('calculate').onclick=getTotal;
}

Okay the HTML code for it:

<form id="form" name="form">
<fieldset id="controls">
<p>Do you like chocolate?
<label>Yes a lot </label>
<input type="radio" name="choco" id="alot" value="Alot" checked="true">

<label>Not that much </label>
<input type="radio" name="choco" id="notMuch" value="NotMuch">
<label>No, but still I don't mind eating it sometimes </label>
<input type="radio" name="choco" id="noSometimes" value="NoSometimes">

 <label>No, I hate it </label>
<input type="radio" name="choco" id="hate" value="Hate">
</p>
<p>Do you prefer chocolate cake or carrot cake?
<label>chocolate </label>
<input type="radio" name="cake" id="chocolate" value="Chocolate" checked="true">

<label>Carrot </label>
<input type="radio" name="cake" id="carrot" value="Carrot">

<label>Both</label>
<input type="radio" name="cake" id="both" value="Both">

<label>None </label>
<input type="radio" name="cake" id="none" value="None">
</p>

<p>
  <input type="button" name="Calculate" id="calculate" value="Calculate" />
</p>
    <p id="result"></p>

    </form>
9
  • what does document.getElementById('calculate') return at the point it is called? Commented Apr 1, 2014 at 22:31
  • the net::ERR_FILE_NOT_FOUND is a network error. Is there another important file that is "included" into your page somehow? Like a css file, another javascript file, etc? And the onclick error message means that the thing you tried to set a property of an object that doesn't exist, nameingly, document.getElementById('calculate') Therefore, I am inclined to think that the calculate element is somehow loaded from another file, which is not being loaded, therefore causing the error message. So, to help us narrow it down, could you tell us all of the files that are being used in this page? Commented Apr 1, 2014 at 22:34
  • @ Daniel A. White, I have also posted the HTML code for it. Commented Apr 1, 2014 at 22:35
  • try entering document.getElementById('calculate') into the console (you can open it by doing CTRL+SHIFT+J in chrome). What does it return? Commented Apr 1, 2014 at 22:35
  • you mean to put the } before the document.getElementById('calculate').onclick=getTotal;, right? Commented Apr 1, 2014 at 22:37

1 Answer 1

2

I'm guessing that your javascript code is located above the HTML code. Therefore, when you try to access document.getElementById('calculate'), the html code has not been parsed yet, so there is no element with id calculate yet, so it returns undefined. Try putting the javascript code at the end of the body tag instead of wherever it is (probably in the head tag, im guessing)

Sign up to request clarification or add additional context in comments.

12 Comments

I have got an external js, that is attached to the head part of HTML, now I have changed the position of { and it looks like this:still javascript code gives nothing back.
function getTotal() { var totalScore = getScoreCake() + getScoreChoco(); document.getElementById('result').innerHTML = "Your total score is: "+totalScore; } document.getElementById('calculate').onclick=getTotal;
that's how it should be, i think
I am still having the error message: Uncaught TypeError: Cannot set property 'onclick' of null , I don't want to sound cheeky, but could you simply copy and paste the html code and the javascript code in your notepad and see what I mean with the error message?
yes, but it's better to post a jsfiddle (see jsfiddle.net) for things like this
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.