Solved

IE8 to replace placeholder and use the focus and blur events

Posted on 2013-12-02
7
878 Views
Last Modified: 2013-12-04
Hi, i have the following code:

<input id="email" placeholder="Hello Friend" title="Hello Friend" name="email" type="text" />

The above code is working fine in all browsers except in IE8,

so for IE i was trying something like this that it should removeplaceholder attribute and use the focus and the blur events to use the title of the input field to display inside the input field

it should apply to all the input fields irrespective of one only

Jquery Code
$(document).ready(function() {
	if ($.browser.msie && parseInt($.browser.version) <= 9) {
		$("input").removeAttr('placeholder');
$('input')
  .on('focus', function(){
      var $this = $(this);
      if($this.val() == $($this.attr('title'))){
          $this.val('');
      }
  })
  .on('blur', function(){
      var $this = $(this);
      if($this.val() == ''){
          $this.val($($this.attr('title')));
      }
  });¿
}
});

Open in new window

i am missing something here, so please guide what i am doing wrong
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 39689483
Hi,
this script works:
<script type="text/javascript">
	$(document).ready(function() {
		if ($.browser.msie && parseInt($.browser.version) <= 9) {
			$("input").removeAttr('placeholder');
			$('input')
				.on('focus', function(){
					var $this = $(this);
					if($this.val() == ($(this).attr('title'))){
						$this.val('');
					}
				})
				.on('blur', function(){
					var $this = $(this);
					if($this.val() == ''){
						$this.val($(this).attr('title'));
					}
				});
		}
	});
</script>

Open in new window


Two addtional infos:
depending on your jQuery version you may have to add the jQuery Migrate library as the $.browser object has been removed since jQuery 1.9.

Second you might think about setting the initial "placeholders" as well - as it looks strange to get something after your first focus/blur on the input field.

HTH
Rainer
0
 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 39689605
i checked in IE8, it is not working giving an error:

Message: 'null' is null or not an object
Line: 76
Char: 3
Code: 0
0
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 39689628
Hi,
hm, I have it running on my IE8.  Could you perhaps provide your HTML and script source?

Here is my sample:
http://jsfiddle.net/EE_RainerJ/7vaLF/

HTH
Rainer
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 39689704
ok, i gt it runniong, issue was i was using JQuery instead of $.

Now the thing is it only appears when the focus is provided and removed, but i need to show it like a placeholder, i mean onload it should show default title and onfcus and onblur then it should continue to do its functionality
0
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 total points
ID: 39689737
Hi,
you simply have to add this:
            $('input').each(function () {
                var $this = $(this);
					if($this.val() == ''){
						$this.val($(this).attr('title'));
					}
            });

Open in new window


I have updated the jsFiddler - http://jsfiddle.net/EE_RainerJ/7vaLF/

HTH
Rainer
0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 39690320
Hi,

   guess I'm sort of confused.   Why are you worried about the placeholder tag?  If a browser doesn't recognize a tag, it simply ignores it.  So, there is no need to 'removeAttr'

Also, what's wrong with setting the input's value and some onblur/onfocus inline statements?


<!DOCTYPE html>
<html>
<head>
<script>
function doIt(obj){
alert(obj.mypersonalID);
}
</script>
</head>
<body>
<form>
<input type='text' mypersonalID = "vp"  onBlur="doIt(this)"/>
<input type="text" value="First Name" onFocus="this.value=''" onBlur='this.value = (this.value=="") ? this.value="First Name": this.value;' />
<input type="text" value="First Name"  placeholder="First Name" onFocus="this.value=''" onBlur='this.value = (this.value=="") ? this.value="First Name": this.value;' />
</form>

</body>
</html>

Open in new window

0
 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 39691786
that is because using placeholder in IE8, it was not showing me the values which were already filled in from backend
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with mod_substitute 18 44
Building JSON/JQuery Results Display 11 27
Boolean 13 24
Make Float not to Wrap 15 28
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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…

726 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