Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
29
rated 0 times [  35] [ 6]  / answers: 1 / hits: 29147  / 11 Years ago, thu, august 15, 2013, 12:00:00

I'm newbie in JavaScript, i hope you can help me, as in topic, null property.



var add = document.getElementById('addition').checked;
var subs = document.getElementById('substraction').checked;
var multi = document.getElementById('multiplication').checked;
var div = document.getElementById('division').checked;

var result = 0;
var x = parseInt(document.getElementById('firstNumber').value);
var y = parseInt(document.getElementById('secondNumber').value);

function calculator()
{
if (add)
{
result = addition(x, y);
}
else if (subs)
{
result = substraction(x, y);
}
else if (multi)
{
result = multiplication(x, y);
}
else if (division)
{
result = division(x, y);
};
}


<fieldset>
<legend>Method</legend>
<p><label><input type=radio name=method id=addition />Addition</label></p>
<p><label><input type=radio name=method id=substraction />Substraction</label></p>
<p><label><input type=radio name=method id=multiplication />Multiplication</label></p>
<p><label><input type=radio name=method id=division />Division</label></p>
</fieldset>

<input type=submit value=Submit onclick=calculator(); />


And them i got message
Uncaught TypeError: Cannot read property 'checked' of null index.html:24
(anonymous function)



Please help me.
Greets!


More From » properties

 Answers
6

Your javascript code is executing before the DOM elements are ready on the page.



You need to execute the code that is trying to get the inputs after the DOM is ready.



(function () {
if (window.addEventListener) {
window.addEventListener('DOMContentLoaded', domReady, false);
} else {
window.attachEvent('onload', domReady);
}
} ());

function domReady() {
var add = document.getElementById('addition').checked;
var subs = document.getElementById('substraction').checked;
var multi = document.getElementById('multiplication').checked;
var div = document.getElementById('division').checked;

var result = 0;
var x = parseInt(document.getElementById('firstNumber').value);
var y = parseInt(document.getElementById('secondNumber').value);
}

[#76334] Wednesday, August 14, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stephonkeandrer

Total Points: 392
Total Questions: 94
Total Answers: 100

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
;