-
-
Save b-durand/5272763 to your computer and use it in GitHub Desktop.
Emulate placeholder HTML5 with JavaScript (for IE less than 10)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setup_placeholders = (function() { | |
$.support.placeholder = false; | |
test = document.createElement('input'); | |
if ('placeholder' in test) { | |
$.support.placeholder = true; | |
return function() {} | |
} else { | |
return function() { | |
$(function() { | |
var active = document.activeElement; | |
$('form').delegate(':text', 'focus', function () { | |
var _placeholder = $(this).attr('placeholder'), | |
_val = $(this).val(); | |
if (_placeholder !== undefined && _val == _placeholder) { | |
$(this).val('').removeClass('hasPlaceholder'); | |
} | |
}).delegate(':text', 'blur', function () { | |
var _placeholder = $(this).attr('placeholder'), | |
_val = $(this).val(); | |
// No need to test for values specific to a particular jQuery version | |
// undefined and an empty string both are falsy | |
if (_placeholder !== undefined && ( _val == '' || _val == _placeholder)) { | |
$(this).val(_placeholder).addClass('hasPlaceholder'); | |
} | |
}).submit(function () { | |
$(this).find('.hasPlaceholder').each(function() { | |
$(this).val(''); | |
}); | |
}); | |
$(':text').blur(); | |
$(active).focus(); | |
}); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment