Solved

IE8 to replace placeholder and use the focus and blur events

Posted on 2013-12-02
7
850 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
Question by:myselfrandhawa
  • 3
  • 3
7 Comments
 
LVL 44

Expert Comment

by:Rainer Jeschor
Comment Utility
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:myselfrandhawa
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 15

Author Comment

by:myselfrandhawa
Comment Utility
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
Comment Utility
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
Comment Utility
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:myselfrandhawa
Comment Utility
that is because using placeholder in IE8, it was not showing me the values which were already filled in from backend
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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)

771 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

12 Experts available now in Live!

Get 1:1 Help Now