Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML how to clear input using javascript?

I have this INPUT, it will clear everytime we click inside of it.

The problem: I want to clear only if value = [email protected]

<script type="text/javascript">     function clearThis(target) {         target.value= "";     } </script> <input type="text" name="email" value="[email protected]" size="30" onfocus="clearThis(this)"> 

Can someone help me to do this? I don't know how to compare, I already tried but no success.

like image 335
Lucas Ferraz Avatar asked Jun 21 '13 14:06

Lucas Ferraz


People also ask

How do you clear the input field in HTML?

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

How do you clear a value in JavaScript?

Reset or clear a form using JavaScript, with the reset() method. The reset method sets the values of all elements in a form like clicking the reset button.


2 Answers

<script type="text/javascript">     function clearThis(target) {         if (target.value == '[email protected]') {             target.value = "";         }     } </script> 

Is this really what your looking for?

like image 97
David Scott Avatar answered Sep 20 '22 07:09

David Scott


For me this is the best way:

<form id="myForm">   First name: <input type="text" name="fname" value="Demo"><br>   Last name: <input type="text" name="lname"><br><br>   <input type="button" onclick="myFunction()" value="Reset form"> </form>       <script> function myFunction() {     document.getElementById("myForm").reset(); } </script>
like image 23
Juan Prendas Avatar answered Sep 21 '22 07:09

Juan Prendas