How to support placeholder attribute in IE8 and 9

HtmlInternet ExplorerPlaceholder

Html Problem Overview


I have a small issue, the placeholder attribute for input boxes is not supported in IE 8-9.

What is the best way to make this support in my project (ASP Net). I am using jQuery. Need I use some other external tools for it?

Is http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html a good solution?

Html Solutions


Solution 1 - Html

You could use this jQuery plugin: https://github.com/mathiasbynens/jquery-placeholder

But your link seems to be also a good solution.

Solution 2 - Html

You can use any one of these polyfills:

These scripts will add support for the placeholder attribute in browsers that do not support it, and they do not require jQuery!

Solution 3 - Html

the $.Browser.msie is not on the latest JQuery anymore... you have to use the $.support

like below:

 <script>
     
     (function ($) {
         $.support.placeholder = ('placeholder' in document.createElement('input'));
     })(jQuery);

    
     //fix for IE7 and IE8
     $(function () {
         if (!$.support.placeholder) {
             $("[placeholder]").focus(function () {
                 if ($(this).val() == $(this).attr("placeholder")) $(this).val("");
             }).blur(function () {
                 if ($(this).val() == "") $(this).val($(this).attr("placeholder"));
             }).blur();

             $("[placeholder]").parents("form").submit(function () {
                 $(this).find('[placeholder]').each(function() {
                     if ($(this).val() == $(this).attr("placeholder")) {
                         $(this).val("");
                     }
                 });
             });
         }
     });
 </script>

Solution 4 - Html

if you use jquery you can do like this. from this site Placeholder with Jquery

$('[placeholder]').parents('form').submit(function() {
  $(this).find('[placeholder]').each(function() {
    var input = $(this);
    if (input.val() == input.attr('placeholder')) {
      input.val('');
    }
  })
});

these are the alternate links

  1. Placeholder jquery library
  2. HTML5 polyfills -- go for placeholder section

Solution 5 - Html

I had compatibility issues with several plugins I tried, this seems to me to be the simplest way of supporting placeholders on text inputs:

function placeholders(){
	//On Focus
	$(":text").focus(function(){
		//Check to see if the user has modified the input, if not then remove the placeholder text
		if($(this).val() == $(this).attr("placeholder")){
			$(this).val("");
		}
	});

	//On Blur
	$(":text").blur(function(){
		//Check to see if the use has modified the input, if not then populate the placeholder back into the input
		if( $(this).val() == ""){
			$(this).val($(this).attr("placeholder"));
		}
	});
}

Solution 6 - Html

$(function(){    
	if($.browser.msie && $.browser.version <= 9){
		$("[placeholder]").focus(function(){
			if($(this).val()==$(this).attr("placeholder")) $(this).val("");
		}).blur(function(){
			if($(this).val()=="") $(this).val($(this).attr("placeholder"));
		}).blur();

		$("[placeholder]").parents("form").submit(function() {
			$(this).find('[placeholder]').each(function() {
				if ($(this).val() == $(this).attr("placeholder")) {
					$(this).val("");
				}
			})
		});
	}
});

try this

Solution 7 - Html

I use thisone, it's only Javascript.

I simply have an input element with a value, and when the user clicks on the input element, it changes it to an input element without a value.

You can easily change the color of the text using CSS. The color of the placeholder is the color in the id #IEinput, and the color your typed text will be is the color in the id #email. Don't use getElementsByClassName, because the versions of IE that don't support a placeholder, don't support getElementsByClassName either!

You can use a placeholder in a password input by setting the type of the original password input to text.

Tinker: http://tinker.io/4f7c5/1

  • JSfiddle servers are down!

*sorry for my bad english

JAVASCRIPT

function removeValue() {
    document.getElementById('mailcontainer')
        .innerHTML = "<input id=\"email\" type=\"text\" name=\"mail\">";
    document.getElementById('email').focus(); }

HTML

<span id="mailcontainer">
    <input id="IEinput" onfocus="removeValue()" type="text" name="mail" value="mail">
</span>

Solution 8 - Html

For others landing here. This is what worked for me:

//jquery polyfill for showing place holders in IE9
$('[placeholder]').focus(function() {
    var input = $(this);
    if (input.val() == input.attr('placeholder')) {
        input.val('');
        input.removeClass('placeholder');
    }
}).blur(function() {
    var input = $(this);
    if (input.val() == '' || input.val() == input.attr('placeholder')) {
        input.addClass('placeholder');
        input.val(input.attr('placeholder'));
    }
}).blur();

$('[placeholder]').parents('form').submit(function() {
    $(this).find('[placeholder]').each(function() {
        var input = $(this);
        if (input.val() == input.attr('placeholder')) {
            input.val('');
        }
    })
});

Just add this in you script.js file. Courtesy of http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html

Solution 9 - Html

Since most solutions uses jQuery or are not this satisfying as I wished it to be I wrote a snippet for myself for mootools.

function fix_placeholder(container){
	if(container == null) container = document.body;
	
	if(!('placeholder' in document.createElement('input'))){
		
		var inputs = container.getElements('input');
		Array.each(inputs, function(input){
		
			var type = input.get('type');
			
			if(type == 'text' || type == 'password'){
				
				var placeholder = input.get('placeholder');
				input.set('value', placeholder);
				input.addClass('__placeholder');
				
				if(!input.hasEvent('focus', placeholder_focus)){
					input.addEvent('focus', placeholder_focus);
				}
				
				if(!input.hasEvent('blur', placeholder_blur)){
					input.addEvent('blur', placeholder_blur);
				}
				
			}
		
		});
		
	}
}

function placeholder_focus(){
	
	var input = $(this);	
	
	if(input.get('class').contains('__placeholder') || input.get('value') == ''){
		input.removeClass('__placeholder');
		input.set('value', '');
	}
	
}

function placeholder_blur(){
	
	var input = $(this);	
	
	if(input.get('value') == ''){
		input.addClass('__placeholder');
		input.set('value', input.get('placeholder'));
	}
	
}

I confess that it looks a bit more MORE than others but it works fine. __placeholder is a ccs-class to make the color of the placeholder text fancy.

I used the fix_placeholder in window.addEvent('domready', ... and for any additinally added code like popups.

Hope you like it.

Kind regards.

Solution 10 - Html

Solution 11 - Html

<input type="text" name="Name" value="Name" onfocus="this.value = ''" onblur=" if(this.value = '') { value = 'Name'}" />

Solution 12 - Html

Add the below code and it will be done.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
    <script src="https://code.google.com/p/jquery-placeholder-js/source/browse/trunk/jquery.placeholder.1.3.min.js?r=6"></script>
    <script type="text/javascript">
    	// Mock client code for testing purpose
		$(function(){
			// Client should be able to add another change event to the textfield
			$("input[name='input1']").blur(function(){ alert("Custom event triggered."); });	
			// Client should be able to set the field's styles, without affecting place holder
			$("textarea[name='input4']").css("color", "red");
			
			// Initialize placeholder
			$.Placeholder.init();
			
            // or try initialize with parameter
			//$.Placeholder.init({ color : 'rgb(255, 255, 0)' });
            
            // call this before form submit if you are submitting by JS
            //$.Placeholder.cleanBeforeSubmit();
		});
    </script>

Download the full code and demo from https://code.google.com/p/jquery-placeholder-js/downloads/detail?name=jquery.placeholder.1.3.zip

Solution 13 - Html

Here is a javascript function that will create placeholders for IE 8 and below and it works for passwords as well:

/* Function to add placeholders to form elements on IE 8 and below */
function add_placeholders(fm) {	
	for (var e = 0; e < document.fm.elements.length; e++) {
		if (fm.elements[e].placeholder != undefined &&
		document.createElement("input").placeholder == undefined) { // IE 8 and below					
			fm.elements[e].style.background = "transparent";
			var el = document.createElement("span");
			el.innerHTML = fm.elements[e].placeholder;
			el.style.position = "absolute";
			el.style.padding = "2px;";
			el.style.zIndex = "-1";
			el.style.color = "#999999";
			fm.elements[e].parentNode.insertBefore(el, fm.elements[e]);
			fm.elements[e].onfocus = function() {
					this.style.background = "yellow";	
			}
			fm.elements[e].onblur = function() {
				if (this.value == "") this.style.background = "transparent";
				else this.style.background = "white";	
			}		
		} 
	}
}

add_placeholders(document.getElementById('fm'))

<form id="fm">
  <input type="text" name="email" placeholder="Email">
  <input type="password" name="password" placeholder="Password">
  <textarea name="description" placeholder="Description"></textarea>
</form>

Solution 14 - Html

    <script>
        if ($.browser.msie) {
            $('input[placeholder]').each(function() {

                var input = $(this);

                $(input).val(input.attr('placeholder'));

                $(input).focus(function() {
                    if (input.val() == input.attr('placeholder')) {
                        input.val('');
                    }
                });

                $(input).blur(function() {
                    if (input.val() == '' || input.val() == input.attr('placeholder')) {
                        input.val(input.attr('placeholder'));
                    }
                });
            });
        }
        ;
    </script>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArbejdsgl&#230;deView Question on Stackoverflow
Solution 1 - Htmlred_alertView Answer on Stackoverflow
Solution 2 - HtmlstarbeamrainbowlabsView Answer on Stackoverflow
Solution 3 - HtmlazoteView Answer on Stackoverflow
Solution 4 - HtmlRavi GadagView Answer on Stackoverflow
Solution 5 - HtmlAlex HarperView Answer on Stackoverflow
Solution 6 - HtmlRejeesh PrarathView Answer on Stackoverflow
Solution 7 - HtmlFlorislwView Answer on Stackoverflow
Solution 8 - HtmlJediCateView Answer on Stackoverflow
Solution 9 - HtmlMarioView Answer on Stackoverflow
Solution 10 - Htmlp1errotView Answer on Stackoverflow
Solution 11 - Htmluser3776645View Answer on Stackoverflow
Solution 12 - HtmlAmanView Answer on Stackoverflow
Solution 13 - HtmlJeff BakerView Answer on Stackoverflow
Solution 14 - HtmlIonut Flavius PogacianView Answer on Stackoverflow