Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

IE8 to replace placeholder and use the focus and blur events

Posted on 2013-12-02
7
Medium Priority
?
911 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

Technology Partners: 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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn how to dynamically set the form action using jQuery.
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)
Suggested Courses

670 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