You can clear input field by onFocus or with a Button in JavaScript. Simply Set the onfocus attribute to NULL using this.value.
<input onfocus=this.value=''>
Or Clearing Input with the help of button.
<button onclick="document.getElementById('InputID').value = ''>
JavaScript clear input
Simple example code.
<!DOCTYPE html>
<html>
<body>
<input type="text" onfocus="this.value=''" value="Click here to clear">
</body>
</html>
Output:
Using button
<button onclick="document.getElementById('myInput').value = ''">Clear input field</button>
<input type="text" value="txt" id="myInput">
Using JavaScript
Clear only if value = [email protected]
<script type="text/javascript">
function clearThis(target) {
if (target.value == '[email protected]') {
target.value = "";
}
}
</script>
<input type="text" name="email" value="[email protected]" size="30" onfocus="clearThis(this)">
Do comment if you have any doubts or suggestions on this Js HTML input tag code.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version