HTML, how to clear input using javascript?

I have this INPUT, it will be cleared every time we click inside it.

Problem: I want to clear only if value = exampleplo@exemplo.com

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

Can someone help me do this? I do not know how to compare, I already tried, but did not succeed.

+6
source share
7 answers
 <script type="text/javascript"> function clearThis(target){ if(target.value==' exemplo@exemplo.com '){ target.value= "";} } </script> 

Is this really what you are looking for?

+17
source

you can use the placeholder attribute

 <input type="text" name="email" placeholder=" exemplo@exemplo.com " size="30" /> 

or try this for older browsers

 <input type="text" name="email" value=" exemplo@exemplo.com " size="30" onblur="if(this.value==''){this.value=' exemplo@exemplo.com ';}" onfocus="if(this.value==' exemplo@exemplo.com '){this.value='';}"> 
+2
source

You do not need to worry about it. Just write

 <input type="text" name="email" placeholder=" exemplo@exemplo.com " size="30"> 

replace value with placeholder

+1
source

You can use the placeholder because it does it for you, but for older browsers that do not support the placeholder, try the following:

 <script> function clearThis(target) { if (target.value == " exemplo@exemplo.com ") { target.value = ""; } } function replace(target) { if (target.value == "" || target.value == null) { target.value == " exemplo@exemplo.com "; } } </script> <input type="text" name="email" value=" exemplo@exemplo.com " size="x" onfocus="clearThis(this)" onblur="replace(this)" /> 

EXPLANATION: When the text field has focus, clear the value. When the text field is not focused, and when the field is empty, replace the value.

I hope this works, I had the same problem, but then I tried it and it worked for me.

+1
source

Try the following:

 <script type="text/javascript"> function clearThis(target){ if(target.value == " exemplo@exemplo.com ") { target.value= ""; } } </script> 

0
source
 <script type="text/javascript"> function clearThis(target){ if (target.value === " exemplo@exemplo.com ") { target.value= ""; } } </script> <input type="text" name="email" value=" exemplo@exemplo.com " size="30" onfocus="clearThis(this)"> 

Try it here: http://jsfiddle.net/2K3Vp/

0
source

instead of clearing the name text, use the placeholder attribute, this is good practice

 <input type="text" placeholder="name" name="name"> 
0
source

Source: https://habr.com/ru/post/947841/


All Articles