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 relat开发者_如何学编程ive. 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()?
Text 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.com/p/jquery-watermark/
精彩评论