views:

3381

answers:

5

I see this all over the web, but was wondering if anyone has the JavaScript code for the EASIEST way to show input value on blur, but hide in on focus.

Thanks!

+5  A: 

HTML

<input type="text" id="txtTest" />

Javascript (unobtrusive)

window.onload = function() {
  applyDefaultValue(document.getElementById('txtName'), 'Enter your name');
  applyDefaultValue(document.getElementById('txtAddress'), 'Enter your address');
}

function applyDefaultValue(elem, val) {
  elem.style.color = '#777';
  elem.value = val;
  elem.onfocus = function() {
    if(this.value == val) {
      this.style.color = '';
      this.value = '';
    }
  }
  elem.onblur = function() {
    if(this.value == '') {
      this.style.color = '#777';
      this.value = val;
    }
  }
}

Live Demo

Josh Stodola
This is a great answer, but how would you make it work on two inputs, lets say: Usename, and Password? When I try to use this code, the first input doesn't erase the value on blur. Thanks!
Michal Kopanski
The code works fine. I've updated it to be easier for multiple inputs.
Josh Stodola
And you can't have a default value on a password field becuase it will use symbols instead of text.
Josh Stodola
Right, it is symbols. Thanks a lot!
Michal Kopanski
A: 

This is what I use on my blog. Just go there and check out the source code behind.

function displaySearchText(text){
    var searchField = document.getElementById('searchField');
    if(searchField != null)
        searchField.value = text;
}

Your input field should look something like this:

<input id='searchField' name='q' onblur='displaySearchText("Search...");' onfocus='displaySearchText("");' onkeydown='performSearch(e);' type='text' value='Search...'/>
Juri
A: 

The simplest approach I know of is the following:

<input 
    name="tb" 
    type="text" 
    value="some text"
    onblur="if (this.value=='') this.value = 'some text'" 
    onfocus="if (this.value=='some text') this.value = ''"  />
Fredrik Mörk
A: 

If you don’t care about valid HTML, you use the placeholder attribute. It will work out of the box on a Safari, and you can add some unobtrusive JS to mimic this behavior in other browsers.

More reading:

And google. ;-)

The solution is similar to the one Josh Stodola posted, but it’s more flexible and universal.

Maciej Łebkowski
I guess not to care about valid HTML is never good, isn't it? Moreover just optimizing for one browser...
Juri
Validator is only a tool. You can ignore it’s output as long as you know what you’re doing. And optimizing for the upcoming standard isn’t so bad after all :)
Maciej Łebkowski
+1  A: 

This answer was adopted from Josh Stodola, but was slightly modified to do the following:

  • Show/Hide Value in Username
  • Show/Hide Value in Password (You will see how and why...)
  • Swap input type from 'text' to 'password'

My code looks like this:

window.onload = function() {
  applyDefaultValue(document.getElementById('txtUser'), 'Username');
  applyPasswordType(document.getElementById('txtPass'), 'Password', 'text');
}

function applyDefaultValue(elem, val) {
  elem.style.color = '#777';
  elem.value = val;
  elem.onfocus = function() {
    if(this.value == val) {
      this.style.color = '';
      this.value = ''; //On focus, make blank
    }
  }
  elem.onblur = function() {
    if(this.value == '') {
      this.value = val; //If it's not in focus, use declared value
    }
  }
}

function applyPasswordType(elem, val, typ) {
  elem.style.color = '#777';
  elem.value = val;
  elem.type = typ;
  elem.onfocus = function() {
    if(this.value == val) {
      this.style.color = '';
      this.type = 'password'; //If in focus, input type will be 'password'
      this.value = '';
    }
  }
  elem.onblur = function() {
    if(this.value == '') {
      this.value = val;
      this.type = 'text'; //On blur, input type will be 'text' in order to show value
    }
  }
}

Not sure if this is the safest or the correct way to do it, but it sure looks nice! Check it out here.

If you know of a better and/or safer way, please let me know!

Thanks, in advance, and of course thanks to Josh Stodola for the code. :)

Michal Kopanski
Cool! Great idea for the password fields.
Josh Stodola
This doesn't work in IE
c0mrade
@c0mrade - Thanks for the tip! The target audience doesn't use IE at all. Nevertheless, which version of the browser are you using?
Michal Kopanski