I know there exists many questions about this, but I dont know what to search.
I have a email field, and I want it to have "Write your email here" and when you click/do onfocus it should disappear. Although if you did not write something and go onblur it should appear again.
Answer: Use the jQuery val() Method You can use the val() method to test or check if inputs are empty in jQuery.
The blur() method removes focus from an element.
In jQuery by using blur() property we can remove focus from input textbox field.
<input id="email" name="email" type="text" value="Write your email here" />
$('#email')
.on('focus', function(){
var $this = $(this);
if($this.val() == 'Write your email here'){
$this.val('');
}
})
.on('blur', function(){
var $this = $(this);
if($this.val() == ''){
$this.val('Write your email here');
}
});
jQuery input placeholder
You should use HTML5 placeholder instead and use a jquery plugin for older browsers
http://diveintohtml5.ep.io/forms.html
Running example here:
http://jsfiddle.net/AamkB/
There are a lot of jquery plugins for placeholder, i used this one:
http://webcloud.se/code/jQuery-Placeholder/
You can use the inline JavaScript tags onblur / onfocus . Maybe not the most 'clean' HTML code but it certainly works in all browsers! (Didn't try IE5.5 / 6.0, but hey, who is using these oldies nowadays?! ^_^)
<input type="text" name="aSweetInputField" value="Your textfield value"
class="anyClass" onclick="if (this.defaultValue==this.value) this.value=''"
onblur="if (this.value=='') this.value=this.defaultValue">
That's it!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With