Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Thanked 0 Times in 0 Posts

    Question Why isn't this code working?

    I'm trying to make the average text box change colours depending on the average. If avg is 50 or below red if 50 or above green. Any advice?


    <TITLE> Grade Calculator </TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    function updateForm(whichInput, max) {
    if (isNaN(whichInput.value) || whichInput.value < 0 || whichInput.value > max)
    alert("The number must be between 0 and " + max);
    whichInput.value = 0; // reset the illegal input value
    whichInput.focus(); // return the cursor to the illegal input
    } else {
    document.Grades.average.value = computeAverage(parseFloat(document.Grades.lessons.value), parseFloat(document.Grades.labs.value),
    function computeAverage(lessons, labs, midterm, final, project)
    var average = (lessons + labs + midterm + final + project);
    return average;
    var averageColour = function(average)
    	if(average < 50){
    		average.style.color = 'red';
    	else if(average > 50){
    		average.style.color = 'green';
    return averageColour;
    Enter your individual grades in the boxes below, and your final average will be automatically computed and displayed.
    <FORM NAME="Grades">
    <TD> Enter your lesson average (out of 5):
    <TD> <INPUT TYPE="Text" NAME="lessons" SIZE=5 VALUE=0
    ONCHANGE="updateForm(document.Grades.lessons, 5);">
    <TD> Enter your lab average (out of 10):
    <TD> <INPUT TYPE="Text" NAME="labs" SIZE=5 VALUE=0
    ONCHANGE="updateForm(document.Grades.labs, 10);">
    <TD> Enter your score on Midterm test (out of 25):
    <TD> <INPUT TYPE="Text" NAME="midterm" SIZE=5 VALUE=0
    ONCHANGE="updateForm(document.Grades.midterm, 25);">
    <TD> Enter your score on Final Exam (out of 35):
    <TD> <INPUT TYPE="Text" NAME="final" SIZE=5 VALUE=0
    ONCHANGE="updateForm(document.Grades.final, 35);">
    <TD> Enter your score on the Project (out of 25):
    <TD> <INPUT TYPE="Text" NAME="project" SIZE=5 VALUE=0
    ONCHANGE="updateForm(document.Grades.project, 25);">
    <TR> <TD COLSPAN=2> <HR> </TR>
    <TD> Your average for the course is:
    <TD> <INPUT TYPE="Text" NAME="average" SIZE=5 VALUE=0

  2. #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Sydney, Australia
    Thanked 932 Times in 919 Posts
    I seem to remember seeing an occasional web page coded like that back when I started creating web pages in 2000. Not many though as apparently whatever languages they were written in had been replaced several years earlier.

    HTML 4 became a standard for HTML back in 1997 and HTML 5 should become a standard in the next year or so. You might consider updating the HTML to use one of those versions. You could then use modern JavaScript to interact with the page to do what you want in place of the historical version for Netscape 3 browser that you appear to be trying to use with more modern browsers.
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  3. #3
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Thanked 1,013 Times in 1,010 Posts
    To help you trouble shot:
    If you put this as the first two things in your first function
    function updateform(whichinput, max) {alert(whichinput);alert(max);
    You will see whichinput gives you [object HTMLInputElement] and not what you wanted. So that's the first problem. You want to send the value entered in this parameter. Answer, use this.value.
    onchange="updateform(this.value, 5);"
    I would erase what the use inputted and set the focus to it. MHO.

    Good luck testing. We are here if you run into any other problems.

    Oh yeah, try this as the first part of the HTML code:
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <meta charset="utf-8" />
    <title> grade calculator </title>
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts