Solved

IE8 to replace placeholder and use the focus and blur events

Posted on 2013-12-02
7
862 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
  • 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 15

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 15

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 15

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

911 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

18 Experts available now in Live!

Get 1:1 Help Now