JavaScript Clearing the field of a form with a button

Sounds easy probably, but not for a beginner programmer :)

I have a simple 3 field form with a submit button and a clear button. This is for a homework assignment, and we have been tasked to get the "Clear Fields" button to work properly. Here are more specific instructions:

"Add the JavaScript code for an anonymous function that's stored in a variable named clear. The function should clear the text boxes by using the $ function to get a Textbox object for each text box and then setting the value property of the textbox to an empty string. Then, add a statement in the onload event handler that attaches the clear function to the click event of the Clear Entries button."

I was able to add the statement to the onload event handler:

window.onload = function () {
        $("calculate").onclick = calculateMpg;
        $("miles").focus();
        $("clear").onclick = clear;
    }

But it is the other part I am having problems with.

Add the JavaScript code for an anonymous function that's stored in a variable named clear:

var clear = function () {
        Object.Method   
        }

Here is my full code so far:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">  
    <title>Calculate MPG</title>
    <link rel="stylesheet" target='_blank' href="mpg.css">
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <script>
        var $ = function (id) {
            return document.getElementById(id);
        }
        var calculateMpg = function () {
            var miles = parseFloat($("miles").value);

            var gallons = parseFloat($("gallons").value);

            if (isNaN(miles)) {
                alert("Miles: This must be a numeric value.");}
                else if (miles <0) {
                    alert("Miles: This number must be greater than 0.");}
                else if (isNaN(gallons)) {
                    alert("Gallons: This must be a numeric value.");}
                else if (gallons <0) {
                    alert("Gallons: This number must be greater than 0.");}
            else {
                var mpg = miles / gallons;
                $("mpg").value = mpg.toFixed(1);
            }
        }
        var clear = function () {
            miles.Text = String.Empty
        }

        window.onload = function () {
            $("calculate").onclick = calculateMpg;
            $("miles").focus();
            $("clear").onclick = clear;
        }
    </script>
</head>
<body>
    <section>
        <h1>Calculate Miles Per Gallon</h1>
        <label for="miles">Miles Driven:</label>
        <input type="text" id="miles"><br>
        <label for="gallons">Gallons of Gas Used:</label>
        <input type="text" id="gallons"><br>
        <label for="mpg">Miles Per Gallon</label>
        <input type="text" id="mpg" disabled><br>
        <label>&nbsp;</label>
        <input type="button" id="calculate" value="Calculate MPG"><br>
        <label>&nbsp;</label>
        <input type="button" id="clear" value="Clear Entries"><br>
    </section>
</body>
</html>

And here is the code we were supplied with to work off of:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Calculate MPG</title>
    <link rel="stylesheet" target='_blank' href="mpg.css">
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>  
    <script>
        var $ = function (id) {
            return document.getElementById(id);
        }
        var calculateMpg = function () {
            var miles = parseFloat($("miles").value);
            var gallons = parseFloat($("gallons").value);

            if (isNaN(miles) || isNaN(gallons)) {
                alert("Both entries must be numeric");
            }
            else {
                var mpg = miles / gallons;
                $("mpg").value = mpg.toFixed(1);
            }
        }
        window.onload = function () {
            $("calculate").onclick = calculateMpg;
            $("miles").focus();
        }
    </script>
</head>
<body>
    <section>
        <h1>Calculate Miles Per Gallon</h1>
        <label for="miles">Miles Driven:</label>
        <input type="text" id="miles"><br>
        <label for="gallons">Gallons of Gas Used:</label>
        <input type="text" id="gallons"><br>
        <label for="mpg">Miles Per Gallon</label>
        <input type="text" id="mpg" disabled><br>
        <label>&nbsp;</label>
        <input type="button" id="calculate" value="Calculate MPG"><br>
    </section>
</body>
</html>
Answer:1

I'm new to Angular JS. Sorry for this simple question. I have learnt something from this tutorial: http://courseware.codeschool.com/shaping-up-with-angular-js/Slides/level01-05.pdf And I have tried ...

I'm new to Angular JS. Sorry for this simple question. I have learnt something from this tutorial: http://courseware.codeschool.com/shaping-up-with-angular-js/Slides/level01-05.pdf And I have tried ...

In this code, I want to display show more link, after 3 buttons: I know, how to display show more button after x words, but how to display show more link after buttons? This is my code to display ...

In this code, I want to display show more link, after 3 buttons: I know, how to display show more button after x words, but how to display show more link after buttons? This is my code to display ...

  1. show read more in jquery
  2. show read more in php
  3. show read more in wordpress
  4. today show read more today
  5. show hide read more javascript
  6. wordpress show read more link
  7. how to show read more in html
  8. javascript hide/show read more for long text
  9. how to show read more link in wordpress post
  10. how to show read more in textview in android
  11. read more show less
  12. read more show hide text
  13. read more show less jquery
  14. read more show less javascript
  15. read more show hide jquery
  16. read more show less css
  17. read more show less bootstrap
  18. read more show hide div
  19. wp-show-posts-read-more
  20. read more. show less. reply

I'm using Rails 4.1.1 and Ruby 2.1.2p95. I am using Slim as my HTML templating language (similar to HAML). I'm using Thin as my Rack server. I am trying to make a fully-AJAX application (one of my ...

I'm using Rails 4.1.1 and Ruby 2.1.2p95. I am using Slim as my HTML templating language (similar to HAML). I'm using Thin as my Rack server. I am trying to make a fully-AJAX application (one of my ...

  1. rails default text
  2. rails textarea default value
  3. rails text default value
  4. rails select default text
  5. rails default value text_field

I want to access the refs from the render function, and set it to the state. Here is my code: export default class App extends Component { constructor(props) { super(); ...

I want to access the refs from the render function, and set it to the state. Here is my code: export default class App extends Component { constructor(props) { super(); ...