Solved

Looking to solve input placeholder IE 8 compatibility issue.

Posted on 2014-07-24
2
2,241 Views
Last Modified: 2014-07-24
I have the following implemented and it's not quite working correctly. And you can only see my solution in IE 8 as it checks if placeholder is compatible because IE 8 doesn't have support for the "placeholder" attribute and I'm trying to figure out a way to solve the issue so that it works in IE 8. You can see what I've started but the last remaining piece is when you focus on the password field it should change the input type to "password" and if you unfocus out of the field with no entry it should default back to "text" input type only if the val = "" or the user didn't actually enter anything. If there's a more streamlined way of what I've already done please share as well. Thx


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

<script>
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;

      var pass = $('<input id="password" class="textbox" name="password" placeholder="Password" type="text">');
      $("#password").replaceWith(pass);

      $(':text').focus(function () {
         if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
            $(this).val('').removeClass('hasPlaceholder');
         }
      }).blur(function () {
         if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
            $(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
         }
      });

      $(document).on('focus click', '#password', function () {
         var pass = $('<input id="password" class="textbox" name="password" placeholder="Password" type="password">');
         $("#password").replaceWith(pass);
      });

   }

   $(active).focus();
   $(':text').blur();

   $('form:eq(0)').submit(function () {
      $(':text.hasPlaceholder').val('');
   });
});
</script>

<style>
   input {width: 150px;}
</style>

</head>
<body>
   <input id="login_id" class="textbox" name="login_id" placeholder="Username" type="text" /><br>
   <input id="password" class="textbox" name="password" placeholder="Password" type="password">
</body>
</html>

Open in new window

0
Comment
Question by:brihol44
2 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 250 total points
ID: 40217711
0
 

Author Comment

by:brihol44
ID: 40217888
Because I didn't know about it. Thx!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now