/    Sign up×
Community /Pin to ProfileBookmark

Please help with JavaScript object error

I need help please. I’m a student and I’m just learning ? . I have to create a grade form (JavaScript/XHTML) that will calculate my grades per unit. I’ve read the text extensively and I’m just not grasping the issue. I validated with W3C validator. But I get the error in browser and it won’t calculate. Additionally I’m not sure if my form syntax is correct. Thanks!

I’m getting an error:
[COLOR=”Navy”]Line: 24
Char:3
Error: ‘document.gradeform.unit1’ is not null or not an object
Code: 0[/COLOR]

=================================================
Here’s the code:

<?xml version=”1.0″ encoding=”utf-8″?>
<!– Name –>
<!– IT 4730 –>
<!– ua301 –>
<!– Unit 3 –>
<!– gradecalc1.html –>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<meta name=”generator” content=”HTML Tidy for Linux (vers 1 September 2005), see www.w3.org” /><!– Page title –>
<title>JavaScript Grade Calculator</title>
<!– Grade Calculator with Loop –>

<script type=”text/javascript”>
//<![CDATA[
function gradeCalc()
{

// Make output variable
var gradeOut;

// Variables for all inputs
var gradeUnit1 = document.gradeform.unit1.value;
var gradeUnit2 = document.gradeform.unit2.value;
var gradeUnit3 = document.gradeform.unit3.value;
var gradeUnit4 = document.gradeform.unit4.value;

// Grades added
gradeOut = “My Calculated Grades:nn”;
gradeOut+= “Unit 1 Grade: ” + gradeUnit1 + “n”;
gradeOut+= “Unit 2 Grade: ” + gradeUnit2 + “n”;
gradeOut+= “Unit 3 Grade: ” + gradeUnit3 + “n”;
gradeOut+= “Unit 4 Grade: ” + gradeUnit4 + “nn”;

// Calculate the course grades
var totalGrade = ( (gradeUnit1*.10) + (gradeUnit2*.07) + (gradeUnit3*.07) + (gradeUnit4*.07) );

//Add course grade to the variable
gradeOut+= “My course grade is: ” + totalGrade + “n”;

// Check for grade letter and add to output
if (totalGrade >= 90)
gradeOut+= “My letter grade is: A”;
else if (totalGrade < 90 && totalGrade >= 80)
gradeOut+= “My letter grade is: B”;
else if (totalGrade < 80 && totalGrade >= 70)
gradeOut+= “My letter grade is: C”;
else if (totalGrade < 70 && totalGrade >= 60)
gradeOut+= “My letter grade is: D”;
else if (totalGrade < 60)
gradeOut+= “My letter grade is: F”;

// Print out all grades in the textarea
document.gradeform.gradeout.value = gradeOut;

}
//]]>
</script>
</head>
<body>
<!– Page header –>
<h1>JavaScript Grade Calculator</h1>
<!– Start the form –>
<form id = “gradeform” action =””>
<!– Table –>
<table border=”0″ cellpadding=”10″>
<tr><!– Table Headers –>
<th>Grade Units</th>
<th>My Grades</th>
</tr>
<tr>
<td valign=”top”><!– Input all the grades –>
Unit 1 Grade:<br />
<input type=”text” name=”unit1″ size=”15″ /><br />
<br />
Unit 2 Grade:<br />
<input type=”text” name=”unit2″ size=”15″ /><br />
<br />
Unit 3 Grade:<br />
<input type=”text” name=”unit3″ size=”15″ /><br />
<br />
Unit 4 Grade:<br />
<input type=”text” name=”unit4″ size=”15″ /><br />
<br />
<input type=”button” value=”Calculate Grades” onclick=”gradeCalc()” /></td>
<td valign=”top”><!– Textarea used to return grades –>
<textarea name=”gradeout” rows=”15″ cols=”25″>
</textarea></td>
</tr>
</table>
</form>
</body>
</html>

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@jasonahouleApr 25.2008 — Try using the parseInt() function on your input.
Copy linkTweet thisAlerts:
@Logic_AliApr 25.2008 — 
I'm getting an error:

[COLOR=Navy]Line: 24

Char:3

Error: 'document.gradeform.unit1' is not null or not an object

Code: 0[/COLOR]


...........

<form id = "gradeform" action ="">
[/quote]

[COLOR=Black][I]document.gradeform [/I][/COLOR][COLOR=Black]references an object with the name 'gradeform' not an ID.





You can give your form the name '[/COLOR]
[COLOR=Black]gradeform[/COLOR][COLOR=Black]', then refence it:

document.forms.gradeform or you can address it via document.getElementById('gradeform')[/COLOR]
Copy linkTweet thisAlerts:
@lilrhinoauthorApr 25.2008 — Try using the parseInt() function on your input.[/QUOTE]

I'll try the parseInt, I saw that in the text that I'm using for the class. Thanks.
Copy linkTweet thisAlerts:
@jasonahouleApr 25.2008 — You have to do what logic said too. I missed that earlier.
×

Success!

Help @lilrhino spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...