Solved

IE8 to replace placeholder and use the focus and blur events

Posted on 2013-12-02
7
873 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 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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 …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

856 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