Code Snippets

Attach Focus and Blur Events With Form Input Elements

jQuery(document).ready(function(){

// for validation purposes
//this function attached focus and blur events with input elements
var addFocusAndBlur = function($input, $val){

		$input.focus(function(){
			if (this.value == $val) {this.value = '';}
		});

		$input.blur(function(){
			if (this.value == '') {this.value = $val;}
		});
	}

// example code to attach the events
addFocusAndBlur(jQuery('#name'),'Name');
addFocusAndBlur(jQuery('#email'),'Email');
addFocusAndBlur(jQuery('#notes'),'Notes');
addFocusAndBlur(jQuery('#link'),'Link');

});

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>