简体   繁体   中英

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 = 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.

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

Is this really what your looking for?

For me this is the best way:

<form id="myForm">
  First name: <input type="text" name="fname"><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>

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='';}">

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.

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

而不是清除名称文本使用占位符属性,这是一种很好的做法

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

Try this :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM