Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a clear button with HTML and JavaScript

I can't figure out why this won't work! My clear button does nothing. Here is my entire form/calculator. I need the "clear" button just to get rid of the text in the "feet" and "meters" fields.

I'm working on a class project.

<body>
<form name="myForm">

<table  border = "0" width = 300px>
<tr>
  <td> 
    Feet:
  </td>
  <td>
    <input type="text" id="feet">
  </td>
</tr>
<tr>
  <td> 
    Meters:
  </td>
  <td>
    <input type="text" id="meters">
</tr>
</table>
</form>
</body>

<p>
<button onClick="calculate()">Convert</button>
<button onClick="clear()">Clear</button>
<p>

<script>

function calculate()
{
  var feet = document.getElementById("feet").value;
  var meters = document.getElementById("meters").value;
  var final;

  if (feet == "" && meters == "")
  {
    alert("Try Again");
    return;
  }
  else if (!(feet == "") && !(meters == ""))
  {
    alert("Try Again");
    return;
  }
  else if (feet == "")
  {
    final = (meters*(3.28084));
    //Display!
    final = final.toFixed(2);
    document.getElementById("feet").value = final;

  }
  else if (meters == "")
  {
    final = (feet*(.3048));
    //Display!
    final = final.toFixed(2);
    document.getElementById("meters").value = final;
  }
} 

//****************************************************************
function clear()
{   
   document.getElementById("myForm").reset();
}

</script>
like image 534
user3093353 Avatar asked Mar 15 '26 17:03

user3093353


2 Answers

The actual problem is that you are using this

document.getElementById("myForm").reset();

but your form has no ID, so you would add the ID

<form name="myForm" id="myForm">

Working demo

That will work for you, but if you want to fix your code read on.

You have a few errors. Your form needs an id because you're using getElementById
There's no need to use a button to submit javascript that does what a button can do.
You have </body> then more HTML.
There's a missing </td>
<table border = "0" width = 300px> is not correct syntax (and you should really use the style attribute or a CSS file)

Also you can use a submit button and bind the function to the action of the form being submitted, which I've done here.

<body>
<form name="myForm" id="myForm">
    <table  border = "0" width = "300">
        <tr>
            <td>Feet:</td>
            <td><input type="text" id="feet"></td></tr>
        <tr>
            <td>Meters:</td>
            <td><input type="text" id="meters"></td>
        </tr>
    </table>
    <button type="submit">Convert</button>
    <button type="reset">Clear</button>
</form>

<script>

document.getElementById("myForm").onsubmit=function(){
    calculate();
    return false;
};

function calculate()
{
  var feet = document.getElementById("feet").value;
  var meters = document.getElementById("meters").value;
  var final;

  if (feet == "" && meters == "")
  {
    alert("Try Again");
    return;
  }
  else if (!(feet == "") && !(meters == ""))
  {
    alert("Try Again");
    return;
  }
  else if (feet == "")
  {
    final = (meters*(3.28084));
    //Display!
    final = final.toFixed(2);
    document.getElementById("feet").value = final;

  }
  else if (meters == "")
  {
    final = (feet*(.3048));
    //Display!
    final = final.toFixed(2);
    document.getElementById("meters").value = final;
  }
} 

</script>
</body>
like image 116
Popnoodles Avatar answered Mar 18 '26 08:03

Popnoodles


<button type="reset" value="Reset">

Why do you have to use JS to accomplish this outside of the JS attached to the clear "button"? I would stick to HTML and just use a simple form reset.

Or, try this here: http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_form_reset

like image 28
ben.kaminski Avatar answered Mar 18 '26 07:03

ben.kaminski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!