Change input type text to password type onclick – JQuery

Sometimes we need to change input type text to password type when user click on it. It should like more attractive. This is very simple. Here is the code for jQuery, just set a class name e.g. pw for that input field:

$('input.pw').each(function() {
		var val = $(this).attr('value');
		if(val != '')
		{
			$(this).focus(function() {
				var newVal = $(this).val();
				if(newVal == val) $(this).val('');
				$(this)[0].type = 'password';
			});
			$(this).blur(function() {
				var newVal = $(this).val();
				if(newVal == '')
				{
					$(this).val(val);
					$(this)[0].type = 'text';
				}
			});
		}
	});
You may also read:  jQuery: Create your own Accordion – Very Easy Tutorial

You may also like

Leave a Reply