"We are what we repeatedly do. Excellence, therefore, is not an act but a habit." - Aristotle   |    "The more difficulties one has to encounter, within and without, the more significant and the higher in inspiration his life will be" - Horace Bushnell   |    "Perl - The only language that looks the same before and after RSA encryption." - Keith Bostic   |    "Good luck' follows careful preparation; 'bad luck' comes from sloppiness." - Robert Heinlein   |    "Our doubts are traitors, and make us lose the good we oft might win, by fearing to attempt." - William Shakespeare   |    "Nothing great was ever achieved without enthusiasm." - Ralph Waldo Emerson   |    "If Java had true garbage collection, most programs would delete themselves upon execution." - Robert Sewell   |    "Nothing can stop the man with the right mental attitude from achieving his goal; nothing on earth can help the man with the wrong mental attitude." - Thomas Jefferson   |    "We are still masters of our fate. We are still captains of our souls." - Winston Churchill   |    "The talent of success is nothing more than doing what you can do, well." - Henry W. Longfellow   |    Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.   |    "Every artist was first an amateur." - Ralph Waldo Emerson   |    "There is no such thing as luck; there is only adequate or inadequate preparation to cope with a statistical universe." - Robert Heinlein   |    "The more you learn, the more you need to learn." - Robert Heinlein   |    "The big secret in life is that there is no big secret. Whatever your goal, you can get there if you're willing to work." - Oprah Winfrey   |    "Take calculated risks. That is quite different from being rash." - George S. Patton   |    "Success is the sum of small efforts, repeated day in and day out." - Robert Collier   |    "Nothing will ever be attempted if all possible objections must first be overcome." - Samuel Johnson   |   

Code Reactor

The best of modern Web development

Drop-in fix for ‘placeholder’ attribute of input tags in Internet Explorer (even IE9)

I’ve been using this drop-in replacement for adding the placeholder support for some time now, but now I noticed that it doesn’t work as expected in IE9. What is worse is that it creates problems: while the placeholder functionality works as advertised, it touches inputs that have no placeholder attribute set and creates all sort of havoc. (In my case users could not log in because the script removed the username from forms just because that input did not have it’s placeholder attribute set.)

Nevertheless, here is the revised version that does work as expected even in IE9: (still requires jQuery)

jQuery(function() {
	jQuery.support.placeholder = false;
	test = document.createElement('input');
	if('placeholder' in test) jQuery.support.placeholder = true;
});
 
$(function() {
	if(!$.support.placeholder) { 
		var active = document.activeElement;
		$(':text').focus(function () {
			if (($(this).attr('placeholder') != "") 
			&& ($(this).val() == $(this).attr('placeholder'))) {
				$(this).val('').removeClass('hasPlaceholder');
			}
		}).blur(function () {
			if (($(this).attr('placeholder') != "") 
			&& (typeof ($(this).attr('placeholder')) == 'string') 
			&& ($(this).val() == "" || $(this).val() == $(this).attr('placeholder'))) {
				$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
			}
		});
		$(':text').blur();
		$(active).focus();
		$('form').submit(function () {
			$(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
		});
	}
});

Leave a Reply

You must be logged in to post a comment.

loading...
Your connection appears to be too slow, automatically disabling HeavyAjax (TM) for better performance...
You seem to run a browser without JavaScript support or it has been disabled. To fully experience Code Reactor please enable JavaScript. (It is not 1995 anymore :)
You seem to be using Internet Explorer. If you want to experience both Code Reactor and the rest of the web to their fullest and fastest, you are advised to download and install a real browser, like Opera, Firefox or Google Chrome.
Close
You seem not only to use Internet Explorer, which is by far a joke when it comes to browsers, but to even use an old version of it!
If you want to experience both Code Reactor and the rest of the web to their fullest and fastest, you are STRONGLY advised to download and install a real browser, like Opera, Firefox or Google Chrome.
Close