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.
This question is related to
javascript
html
input
erase
you can use attribute placeholder
<input type="text" name="email" placeholder="[email protected]" size="30" />
or try this for older browsers
<input type="text" name="email" value="[email protected]" size="30" onblur="if(this.value==''){this.value='[email protected]';}" onfocus="if(this.value=='[email protected]'){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 == "[email protected]") {
target.value = "";
}
}
function replace(target) {
if (target.value == "" || target.value == null) {
target.value == "[email protected]";
}
}
</script>
<input type="text" name="email" value="[email protected]" 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.
<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)">
Try it out here: http://jsfiddle.net/2K3Vp/
You don't need to bother with that. Just write
<input type="text" name="email" placeholder="[email protected]" size="30">
replace the value with placeholder
Try this :
<script type="text/javascript">
function clearThis(target){
if(target.value == "[email protected]")
{
target.value= "";
}
}
</script>
instead of clearing the name text use placeholder attribute it is good practice
<input type="text" placeholder="name" name="name">
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>
Source: Stackoverflow.com