Solved

Looking to solve input placeholder IE 8 compatibility issue.

Posted on 2014-07-24
2
2,282 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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…

840 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