Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript text input fields that display instruction (placeholder) text

What's the best way to have a text input field that displays instructions of what to enter in that field in gray. When you focus on that input field, the gray instruction text disappears and your input text appears black. If you erase your input text and focus away from that input, then the instruction gray text reappears.

I've tried implementing this with jQuery, but I get all kinds of weird issues with different browsers. Sometimes when you go back a screen, the instruction text becomes black and no longer disappears, issues like that.

Is there a standard javascript library for this? I can't find any!

Oh, and the instruction text can't be an image because it has to be available in different languages depending on the browser's language preference.

like image 375
at. Avatar asked Aug 10 '10 05:08

at.


3 Answers

You don't need javascript for this.

HTML5:

<input type="text" id="email" name="email" placeholder="Enter your email address">

This works in modern browsers. If you add modernizr.js to your page then it'll work in all browsers.

like image 60
autonomatt Avatar answered Sep 28 '22 10:09

autonomatt


You can use watermark plugin in jquery.use this link.

example

 $(this).Watermark("your instructions");
like image 36
Amit Soni Avatar answered Sep 28 '22 11:09

Amit Soni


html

<input type="text" id="user" class="textbox default" value="Enter login name">
<input type="password" id="pass" class="textbox">

jQuery

$(function(){
  $('.textbox').focus(function(){
    if (this.value == this.defaultValue) {
        this.value = '';
        $(this).removeClass('default');
    };
  }).blur(function(){
    if (this.value == '') {
        this.value = this.defaultValue;
        $(this).addClass('default');
    };
  });​
});

demo

like image 42
Reigel Avatar answered Sep 28 '22 12:09

Reigel