1

I'm using the framework of Materialize from materializecss.com. The placeholder for the textbox isn't working on IE, I think the framework has something to do with this because from my other page I tried using bootstrap framework, and the placeholder property worked, is there any solution for me to make the placeholder property work on the Materialize framework?

<div class="row">
  <div class="input-field col s10">
     <asp:TextBox ID="txtCompany" runat="server" Visible="false" placeholder="Company Name" ></asp:TextBox>                  
   </div>
  <div class="input-field col s2">
     <asp:Button ID="btnSave" runat="server" Text="Save" CssClass="btn waves-effect waves-red" OnClick="btnSave_Click" Visible="false" />
  </div>
</div>
JC Borlagdan
  • 3,318
  • 5
  • 28
  • 51

2 Answers2

4

You need to add onfocus and onblur property in your textbox for working it in Internet Explorer

Here you go

<div class="input-field col s10">
    <asp:TextBox ID="txtCompany" runat="server" value="Company Name" placeholder="Company Name" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"></asp:TextBox>
</div>
Nad
  • 4,605
  • 11
  • 71
  • 160
  • try removing `placeholder` property and check it is working fine on my side – Nad Apr 28 '15 at 08:56
  • oh it's actually working, but no design, but it's okay at least it still answered my question. Thanks for the immediate reply nadeem – JC Borlagdan Apr 28 '15 at 08:58
  • I guess, your `Materilize` css may be conflictiing. Glad that helped..!! – Nad Apr 28 '15 at 08:59
  • 1
    yes probably, btw, can you explain the onfocus and onblur property you added? – JC Borlagdan Apr 28 '15 at 09:00
  • You can have a look here [onfocus](http://www.w3schools.com/jsref/event_onfocus.asp) and [onblur](http://www.w3schools.com/tags/ev_onblur.asp). Hope that makes your logic clear.! – Nad Apr 28 '15 at 09:02
1

Placeholder text for form elements was never implemented for IE9, Placeholder support was added in IE10.

You can either use

HTML5 Placeholder jQuery Plugin - by Mathias Bynens (a collaborator on HTML5 Boilerplate and jsPerf)

https://github.com/mathiasbynens/jquery-placeholder

Demo & Examples

http://mathiasbynens.be/demo/placeholder

or you can use the code below to do the same think without jquery. This code was taken from https://stackoverflow.com/a/9109448/394381 written by "Mark Rhodes"

(function(){

     "use strict";

     //shim for String's trim function..
     function trim(string){
         return string.trim ? string.trim() : string.replace(/^\s+|\s+$/g, "");
     }

     //returns whether the given element has the given class name..
     function hasClassName(element, className){ 
         //refactoring of Prototype's function..
         var elClassName = element.className;
         if(!elClassName)
             return false;
         var regex = new RegExp("(^|\\s)" + className + "(\\s|$)");
         return regex.test(element.className);
     }

     function removeClassName(element, className){
         //refactoring of Prototype's function..
         var elClassName = element.className;
         if(!elClassName)
             return;
         element.className = elClassName.replace(
             new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' ');
     }

     function addClassName(element, className){
         var elClassName = element.className;
         if(elClassName)
             element.className += " " + className;
         else
             element.className = className;
     }

     //strings to make event attachment x-browser.. 
     var addEvent = document.addEventListener ?
            'addEventListener' : 'attachEvent';
     var eventPrefix = document.addEventListener ? '' : 'on';

     //the class which is added when the placeholder is being used..
     var placeHolderClassName = 'usingPlaceHolder';

     //allows the given textField to use it's placeholder attribute
     //as if it's functionality is supported natively..
     window.placeHolder = function(textField){

         //don't do anything if you get it for free..
         if('placeholder' in document.createElement('input'))
             return;

         //don't do anything if the place holder attribute is not
         //defined or is blank..
         var placeHolder = textField.getAttribute('placeholder');        
         if(!placeHolder)
             return;

         //if it's just the empty string do nothing..
         placeHolder = trim(placeHolder);
         if(placeHolder === '')
             return;

         //called on blur - sets the value to the place holder if it's empty..
         var onBlur = function(){
             if(textField.value !== '') //a space is a valid input..
                 return;
             textField.value = placeHolder;
             addClassName(textField, placeHolderClassName);
         };

         //the blur event..
         textField[addEvent](eventPrefix + 'blur', onBlur, false);

         //the focus event - removes the place holder if required..
         textField[addEvent](eventPrefix + 'focus', function(){
             if(hasClassName(textField, placeHolderClassName)){
                removeClassName(textField, placeHolderClassName);
                textField.value = "";
             }
         }, false);

         //the submit event on the form to which it's associated - if the
         //placeholder is attached set the value to be empty..
         var form = textField.form;
         if(form){
             form[addEvent](eventPrefix + 'submit', function(){
                 if(hasClassName(textField, placeHolderClassName))
                     textField.value = '';
            }, false);
         }

         onBlur(); //call the onBlur to set it initially..
    };

}());

For each text field you want to use it for you need to run placeHolder(HTMLInputElement)

Community
  • 1
  • 1
Ali Khalid
  • 1,345
  • 8
  • 19
  • Yes I agree with you but in case the original poster wants to keep the placeholder tag, I guess he could use the solution i posted. – Ali Khalid Apr 28 '15 at 11:02