Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author: Radosław Adamowicz
- (function($)
- {
- $.fn.placeholder = function(styles) // defines styles for pseudo-placeholder, like for example opacity, color, weight etc.
- {
- var helper = {
- styles : null,
- checkPlaceholderSupport : function() // check if placeholder is working
- {
- var test = document.createElement('input');
- return ('placeholder' in test);
- },
- placeholder : function(handle) // put text cursor on the beginning
- {
- var pU = handle.createTextRange();
- pU.collapse(true);
- pU.select();
- },
- cssChanger : function(handle, switcher)
- {
- if(typeof helper.styles === 'object')
- {
- if(switcher == 'create')
- handle.css(styles);
- else if(switcher == 'remove')
- handle.removeAttr('style');
- }
- },
- };
- helper.styles = styles;
- if(helper.checkPlaceholderSupport())
- return false;
- $(this).find('[placeholder]').each(function()
- {
- $ele = $(this);
- $placeholder = $ele.attr('placeholder');
- if($placeholder == '')
- return false;
- $ifPassword = $ele.attr('type');
- $ele.data('type', $ifPassword);
- if($ifPassword == 'password') // check if type == password
- $($ele.attr('type', 'text'));
- $ele.data('placeholder', $placeholder);
- helper.cssChanger($ele, 'create');
- $ele.val($placeholder);
- $ele.on('focus, click', function()
- {
- if($(this).val() == $(this).data('placeholder'))
- helper.placeholder(this);
- }).on('keydown', function()
- {
- if($(this).val() == $(this).data('placeholder'))
- {
- helper.cssChanger($(this), 'remove');
- $(this).val('');
- if($(this).data('type') == 'password')
- $(this).attr('type', 'password');
- }
- }).on('keyup', function()
- {
- if($(this).val() == '')
- {
- helper.cssChanger($(this), 'create');
- $(this).val($(this).data('placeholder'));
- if($(this).data('type') == 'password' && $(this).attr('type') == 'password')
- $(this).attr('type', 'text');
- helper.placeholder(this);
- }
- }).on('blur', function()
- {
- if($(this).val() == '')
- {
- helper.cssChanger($(this), 'create');
- if($(this).data('type') == 'password' && $(this).attr('type') == 'password')
- $(this).attr('type', 'text');
- $(this).val($(this).data('placeholder'));
- }
- });
- $ele.parents('form').on('submit', function()
- {
- $(this).find('[placeholder]').each(function()
- {
- helper.cssChanger($(this), 'remove');
- if($(this).val() == $(this).data('placeholder'))
- $(this).val('');
- });
- });
- });
- return this;
- }
- })(jQuery);
- $(function()
- {
- $('body').placeholder();
- });
Advertisement
Add Comment
Please, Sign In to add comment