So I have this contact form and I want to put text inside each input, when the user clicks on it the text disappears so they can add stuff - when the user clicks on something else it reappears. I have done this with labels and negative margin along with position relative. I plan on adding an onclick function to each input.
My question is - is there a function that is like Offclick()? Or something similiar to mouseOver() and MouseOut()?
So I have this contact form and I want to put text inside each input, when the user clicks on it the text disappears so they can add stuff - when the user clicks on something else it reappears. I have done this with labels and negative margin along with position relative. I plan on adding an onclick function to each input.
My question is - is there a function that is like Offclick()? Or something similiar to mouseOver() and MouseOut()?
Share Improve this question edited Aug 3, 2011 at 19:26 Sean Vieira 160k34 gold badges320 silver badges296 bronze badges asked Aug 3, 2011 at 19:13 Howdy_McGeeHowdy_McGee 10.7k30 gold badges116 silver badges191 bronze badges 1- possible duplicate of mouse click somewhere else on page (not on a specific div) – jAndy Commented Aug 3, 2011 at 19:17
2 Answers
Reset to default 2Text inputs have onblur
and select boxes have onchange
events that can be wired into.
<input type="text" id="UserName" name="UserName" value="Placeholder ..." />
<script type="text/javascript">
var el = document.getElementById("UserName");
var original_text = el.value;
el.onblur = function() {
// `this` is set to the input element by the browser
if ( ! this.value ) {
this.value = original_text;
}
};
</script>
With jQuery, it is even easier:
var el = $("#UserName");
var original_text = el.val();
el.blur(function(){
// `this` is set to the input element by jQuery.
if ( ! this.value ) {
this.value = original_text;
}
});
set title attribute same as value for textboxes and then write:
$("input[type=text]").bind("focus",function(){
var $this = $(this);
if($this.attr("title") ==$this.val()){
$this.val('');
}).bind("blur",function(){
var $this = $(this);
if($this.val() ==''){
$this.val($this.attr("title"));
});
or you could use a ready made solution like : http://code.google./p/jquery-watermark/