44

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

The problem: I want to clear only if value = exemplo@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 to do this? I don't know how to compare, I already tried but no success.

Brian Burns
  • 20,575
  • 8
  • 83
  • 77
Lucas Ferraz
  • 620
  • 1
  • 7
  • 12

9 Answers9

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

Is this really what your looking for?

Bugaloo
  • 1,671
  • 3
  • 16
  • 21
David Scott
  • 944
  • 8
  • 11
17

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>
Carson
  • 6,105
  • 2
  • 37
  • 45
Juan Prendas
  • 171
  • 1
  • 2
  • 2
    Welcome to SO! Please, consider adding an explanation of why you think this would be a solution to OP's question as it is concerned with resetting the input value under a specific condition. This is especially important since other answers already cover the question. Do take a look at answering [guidelines](https://stackoverflow.com/help/how-to-answer) – Oleg Valter is with Ukraine May 25 '20 at 03:34
3

you can use attribute placeholder

<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='';}">
Bugaloo
  • 1,671
  • 3
  • 16
  • 21
3

You could use a placeholder because it does it for you, but for old browsers that don't support placeholder, try this:

<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)" />

CODE EXPLAINED: When the text box has focus, clear the value. When text box is not focused AND when the box is blank, replace the value.

I hope that works, I have been having the same issue, but then I tried this and it worked for me.

SubLock69
  • 91
  • 1
  • 7
0

Try this :

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

Raitom
  • 33
  • 1
  • 5
0
<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 out here: http://jsfiddle.net/2K3Vp/

Adam
  • 2,204
  • 2
  • 15
  • 15
0

You don't need to bother with that. Just write

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

replace the value with placeholder

Duck
  • 9
  • 1
0

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

<input type="text" placeholder="name"  name="name">
saigopi.me
  • 14,011
  • 2
  • 83
  • 54
0

I'm surprised out of all these answers no-one's mentioned the simplest modern way to do this:

<input type="text" placeholder="Your Name" onfocus="this.placeholder=''" onblur="this.placeholder='Your Name'"

The onblur is only necessary if you want to restore the original placeholder after the user clicks away from the input.

Hashim Aziz
  • 4,074
  • 5
  • 38
  • 68