Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing a text field on button click

Tags:

html

I have two text labels:

<div>           
    <input type="text" id="textfield1" size="5"/>
</div>

<div>          
    <input type="text" id="textfield2" size="5"/>           
</div>

I would like to have a button called "Clear" that would erase both these fields.

To my knowledge, I know that to implement a button I should use a code like this:

<div>      
    <input type="button" value="Clear Fields" onclick=SOMETHING />
</div>

Any clues on how to implement SOMETHING?

like image 562
user1415780 Avatar asked Feb 12 '13 16:02

user1415780


People also ask

How do you reset input field on click?

To clear an input field after submitting:Add a click event listener to a button. When the button is clicked, set the input field's value to an empty string. Setting the field's value to an empty string resets the input.

How do you clear a textbox in HTML?

To clear all the input in an HTML form, use the <input> tag with the type attribute as reset.


1 Answers

A simple JavaScript function will do the job.

function ClearFields() {

     document.getElementById("textfield1").value = "";
     document.getElementById("textfield2").value = "";
}

And just have your button call it:

<button type="button" onclick="ClearFields();">Clear</button>
like image 155
shauneba Avatar answered Sep 20 '22 17:09

shauneba