0

pretty new to javascript and doing some examples from a book, can't understand why this isn't producing any text strings on the page when loaded.

function init()
{
var sum = 80 + 20;
var sub = sum - 50;
var mul = sum * 5;
var div = sum / 4;
var mod = sum % 2;
var inc = ++sum;
var dec = --sum;

var str = "Sum: " + sum;
str += "<br>Subtraction: " + sub;
str += "<br>Multiplication: " + mul;
str += "<br>Division: " + div;
str += "<br>Modulus: " + mod;
str += "<br>Increment: " + inc;
str += "<br>Decrement: " + dec;

document.getElementById( "Panel" ).innerHTML = str;
}
document.addEventListener("DOMContentLoaded" , init , false);

And the html5 code;

 <!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="arithmetic.js"></script>
<title>Doing Arithmetic</title>
</head>

<body>

<div id="panel"> </div>

</body>

</html>
3
  • 1
    Panel in your Javascript code should probably be in lowercase so it matches :) Commented Oct 3, 2013 at 9:51
  • wow. what an idiot. i have read over every letter for the last half an hour, didn't notice that though! Thanks very much everyone! Commented Oct 3, 2013 at 9:53
  • Personally I would change the HTML to match the JS id. No-one likes capitalised html attributes. :) Commented Oct 3, 2013 at 9:54

2 Answers 2

3

Your <div> has an id of panel, not Panel. The IDs need to match. So you can either change the JavaScript code:

document.getElementById( "panel" )

or the HTML:

<div id="Panel"> </div>
Sign up to request clarification or add additional context in comments.

Comments

0

Replace

<div id="panel"> </div>

with

<div id="Panel"> </div>

and it will work. P in panel should be capital.

Comments

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.