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
?
This question is related to
html
If you are trying to "Submit and Reset" the the "form" with one Button click, Try this!
Here I have used jQuery function, it can be done by simple JavaScript also...
<form id="form_data">
<input type="anything" name="anything" />
<input type="anything" name="anything" />
<!-- Save and Reset button -->
<button type="button" id="btn_submit">Save</button>
<button type="reset" id="btn_reset" style="display: none;"></button>
</form>
<script type="text/javascript">
$(function(){
$('#btn_submit').click(function(){
// Do what ever you want
$('#btn_reset').click(); // Clicking reset button
});
});
</script>
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>
Something like this will add a button and let you use it to clear the values
<div>
<input type="text" id="textfield1" size="5"/>
</div>
<div>
<input type="text" id="textfield2" size="5"/>
</div>
<div>
<input type="button" onclick="clearFields()" value="Clear">
</div>
<script type="text/javascript">
function clearFields() {
document.getElementById("textfield1").value=""
document.getElementById("textfield2").value=""
}
</script>
If you want to reset it, then simple use:
<input type="reset" value="Reset" />
But beware, it will not clear out textboxes that have default value
. For example, if we have the following textboxes and by default, they have the following values:
<input id="textfield1" type="text" value="sample value 1" />
<input id="textfield2" type="text" value="sample value 2" />
So, to clear it out, compliment it with javascript:
function clearText()
{
document.getElementById('textfield1').value = "";
document.getElementById('textfield2').value = "";
}
And attach it to onclick
of the reset button:
<input type="reset" value="Reset" onclick="clearText()" />
Source: Stackoverflow.com