-1

Here's the full code, along with the script. When I open the html file, the page looks the way I want it to look, but when I enter a value and click the submit button, nothing happens. Is there something wrong in the script, or is it some simple error I missed?

<!DOCTYPE html>
    <html>

<head>

<title>Question 10.10</title>

<style type="text/css"> 
table, table td   { padding: 5px }
</style>

<script type="text/javascript>">
        // initialize 4 rows 5 columns sales array
        var counters = [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ];

        // function to collect and process sales slips
        function submitSales()
        {   
            var salary = 0; // current calculated salary

            // Get values from XHTML form fields
            var grossSales = parseInt( document.getElementById( "grossField" ).value );

            // Validate user input gross
            if ( isNaN( grossSales ) )
            {
                // Alert user to the error
                window.alert( "Invalid gross entered!" );
                return;
            } // End while

            // Calculate salary
            salary = 200 + parseInt( ( .09 * grossSales ) );

            // Increment the counter for the salary range
            if ( salary >= 200 && salary <= 299 )
                ++counters[ 0 ];
            if ( salary >= 300 && salary <= 399 )
                ++counters[ 1 ];
            if ( salary >= 400 && salary <= 499 )
                ++counters[ 2 ];
            if ( salary >= 500 && salary <= 599 )
                ++counters[ 3 ];
            if ( salary >= 600 && salary <= 699 )
                ++counters[ 4 ];
            if ( salary >= 700 && salary <= 799 )
                ++counters[ 5 ];
            if ( salary >= 800 && salary <= 899 )
                ++counters[ 6 ];
            if ( salary >= 900 && salary <= 999 )
                ++counters[ 7 ];
            if ( salary >= 1000 )
                ++counters[ 8 ];

            // output the results
            displayResults();

            // Reset fields in XHTML form
            document.getElementById( "grossField" ).value = "0";

        } // end function submitSales

        // function to generate and display the weekly report
        function displayResults()
        {   
            var printoutTable = "<table border = \"1\">"; // this will hold the table to be output
            printoutTable += "<tr><td colspan = \"2\" style = \"text-align: center\"><h1>Weekly Salary Range Report</h1></td></tr>";
            printoutTable += "<tr><td>Range</td><td>Number of Employees</td></tr>";
            printoutTable += "<tr><td>$200-299</td><td>" + counters[ 0 ] + "</td></tr>";
            printoutTable += "<tr><td>$300-399</td><td>" + counters[ 1 ] + "</td></tr>";
            printoutTable += "<tr><td>$400-499</td><td>" + counters[ 2 ] + "</td></tr>";
            printoutTable += "<tr><td>$500-599</td><td>" + counters[ 3 ] + "</td></tr>";
            printoutTable += "<tr><td>$600-699</td><td>" + counters[ 4 ] + "</td></tr>";
            printoutTable += "<tr><td>$700-799</td><td>" + counters[ 5 ] + "</td></tr>";
            printoutTable += "<tr><td>$800-899</td><td>" + counters[ 6 ] + "</td></tr>";
            printoutTable += "<tr><td>$900-999</td><td>" + counters[ 7 ] + "</td></tr>";
            printoutTable += "<tr><td>$1000+</td><td>" + counters[ 8 ] + "</td></tr>";
            printoutTable += "</table>";

            // send the XHTML code to the div to display the table
            document.getElementById( "salesPrintout" ).innerHTML = printoutTable;

        } 
</script>

</head>



<body>

<form action="">

<h1>Employee Sales</h1>
<td>Gross Sales: $</td>

<td>
<input id="grossfeild" type="text"></input>
</td>

<input type="button" onclick="submitSales()" Value="Submit"></input>

<div id="salesPrintout">

    <table borders="1">

    <h1>Weekly Sales Report</h1>
    <tr>
    <td>Range</td>
    <td>Number of Employees</td>
    </tr>

    <tr>
    <td>$200-299</td>
    <td>0</td>
    </tr>
    <tr>
    <td>$300-399</td>
    <td>0</td>
    </tr>
    <tr>
    <td>$400-499</td>
    <td>0</td>
    </tr>
    <tr>
    <td>$500-599</td>
    <td>0</td>
    </tr>
    <tr>
    <td>$600-699</td>
    <td>0</td>
    </tr>
    <tr>
    <td>$700-799</td>
    <td>0</td>
    </tr>
    <tr>
    <td>$800-899</td>
    <td>0</td>
    </tr>
    <tr>
    <td>$900-999</td>
    <td>0</td>
    </tr>
    <tr>
    <td>$1000+</td>
    <td>0</td>
    </tr>






</body>
</html>
  • The document is not ready. Try to make these actions when the window is loaded. – Klaider Nov 29 '15 at 17:13
  • Possible duplicate of [Why does jQuery or a DOM method such as getElementById not find the element?](http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – baao Nov 29 '15 at 17:13
  • Asked many times before... See the answer above – baao Nov 29 '15 at 17:14

1 Answers1

3
  • First of all, always use td inside of table:

    <table>
        <tr>
            <td>Gross Sales: $</td>
            <td><input id="grossfield" type="text"></input></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="button" onclick="submitSales()" Value="Submit"></input></td>
        </tr>
    </table>
    
  • Second, you had a typo in the script tag: <script type="text/javascript>"> should be <script type="text/javascript">.
    (Btw, javascript is the official standard scripting-language so <script>...</script> is completely sufficient.

  • Third, there's a typo when referring to the gross-field: <input id="grossfeild" type="text"></input> should be <input id="grossfield" type="text"></input>.
    Fyi, classes and ids in javascript are case-insensitive, so it's ok to reference yours with document.getElementById( "grossField" ).

  • Fourth, HTML tags need to be closed.

This is a running example:

// initialize 4 rows 5 columns sales array
    var counters = [0, 0, 0, 0, 0, 0, 0, 0, 0];

// function to collect and process sales slips
    function submitSales()
    {
        var salary = 0; // current calculated salary

// Get values from XHTML form fields
        var grossSales = parseInt(document.getElementById("grossfield").value);

// Validate user input gross
        if (isNaN(grossSales))
        {
// Alert user to the error
            window.alert("Invalid gross entered!");
            return;
        } // End while

// Calculate salary
        salary = 200 + parseInt((.09 * grossSales));

// Increment the counter for the salary range
        if (salary >= 200 && salary <= 299)
            ++counters[ 0 ];
        if (salary >= 300 && salary <= 399)
            ++counters[ 1 ];
        if (salary >= 400 && salary <= 499)
            ++counters[ 2 ];
        if (salary >= 500 && salary <= 599)
            ++counters[ 3 ];
        if (salary >= 600 && salary <= 699)
            ++counters[ 4 ];
        if (salary >= 700 && salary <= 799)
            ++counters[ 5 ];
        if (salary >= 800 && salary <= 899)
            ++counters[ 6 ];
        if (salary >= 900 && salary <= 999)
            ++counters[ 7 ];
        if (salary >= 1000)
            ++counters[ 8 ];

// output the results
        displayResults();

// Reset fields in XHTML form
        document.getElementById("grossfield").value = "0";

    } // end function submitSales

// function to generate and display the weekly report
    function displayResults()
    {
        var printoutTable = "<table border = \"1\">"; // this will hold the table to be output
        printoutTable += "<tr><td colspan = \"2\" style = \"text-align: center\"><h1>Weekly Salary Range Report</h1></td></tr>";
        printoutTable += "<tr><td>Range</td><td>Number of Employees</td></tr>";
        printoutTable += "<tr><td>$200-299</td><td>" + counters[ 0 ] + "</td></tr>";
        printoutTable += "<tr><td>$300-399</td><td>" + counters[ 1 ] + "</td></tr>";
        printoutTable += "<tr><td>$400-499</td><td>" + counters[ 2 ] + "</td></tr>";
        printoutTable += "<tr><td>$500-599</td><td>" + counters[ 3 ] + "</td></tr>";
        printoutTable += "<tr><td>$600-699</td><td>" + counters[ 4 ] + "</td></tr>";
        printoutTable += "<tr><td>$700-799</td><td>" + counters[ 5 ] + "</td></tr>";
        printoutTable += "<tr><td>$800-899</td><td>" + counters[ 6 ] + "</td></tr>";
        printoutTable += "<tr><td>$900-999</td><td>" + counters[ 7 ] + "</td></tr>";
        printoutTable += "<tr><td>$1000+</td><td>" + counters[ 8 ] + "</td></tr>";
        printoutTable += "</table>";

// send the XHTML code to the div to display the table
        document.getElementById("salesPrintout").innerHTML = printoutTable;

    }
table, table td   { padding: 5px }
<!DOCTYPE html>
<html>
    <head>
        <title>Question 10.10</title>
    </head>
    <body>

        <form action="">

            <h1>Employee Sales</h1>
            <table>
                <tr>
                    <td>Gross Sales: $</td>
                    <td><input id="grossfield" type="text" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" onclick="submitSales()" Value="Submit" /></td>
                </tr>
            </table>

            <div id="salesPrintout">

                <table border="1">

                    <h1>Weekly Sales Report</h1>
                    <tr>
                        <td>Range</td>
                        <td>Number of Employees</td>
                    </tr>

                    <tr>
                        <td>$200-299</td>
                        <td>0</td>
                    </tr>
                    <tr>
                        <td>$300-399</td>
                        <td>0</td>
                    </tr>
                    <tr>
                        <td>$400-499</td>
                        <td>0</td>
                    </tr>
                    <tr>
                        <td>$500-599</td>
                        <td>0</td>
                    </tr>
                    <tr>
                        <td>$600-699</td>
                        <td>0</td>
                    </tr>
                    <tr>
                        <td>$700-799</td>
                        <td>0</td>
                    </tr>
                    <tr>
                        <td>$800-899</td>
                        <td>0</td>
                    </tr>
                    <tr>
                        <td>$900-999</td>
                        <td>0</td>
                    </tr>
                    <tr>
                        <td>$1000+</td>
                        <td>0</td>
                    </tr>
                </table>
            </div>
        </form>
    </body>
</html>
doABarrelRoll721
  • 368
  • 2
  • 10