views:

440

answers:

3

i have an online form:

http://yoursdproperty.com/index.php?option=com_chronocontact&Itemid=56

i would like to have some faint grey text there so that when the user clicks on it, it disappears

exactly like in this stackoverflow form where in the title of the question it says "what's your programming question, be descriptive?"

whats the simplest way to implement this?

+4  A: 

You made no mention of jQuery, or any other javascript framework, so I'm going to give you the pure Javascript solution.

Some boolean logic based upon the value of the box to set the font color. When the user clicks the input, if the value is equal to your default value, you erase the contents. If it's not, you do nothing. When the user leaves the box, if the values are empty, add your default text in again.

// Reference our element
var txtContent  = document.getElementById("content");
// Set our default text
var defaultText = "Please enter a value.";

// Set default state of input
txtContent.value = defaultText;
txtContent.style.color = "#CCC";

// Apply onfocus logic
txtContent.onfocus = function() {
  // If the current value is our default value
  if (this.value == defaultText) {
    // clear it and set the text color to black
    this.value = "";
    this.style.color = "#000";
  }
}

// Apply onblur logic
txtContent.onblur = function() {
  // If the current value is empty
  if (this.value == "") {
    // set it to our default value and lighten the color
    this.value = defaultText;
    this.style.color = "#CCC";
  }
}
Jonathan Sampson
can this be done in html?
I__
No, you cannot do this in solely HTML.
emddudley
+2  A: 

You can use this jQuery plugin.

SLaks
+2  A: 

In HTML5, this is achieved very easily via the placeholder attribute - not sure how widely supported that is right now, though.

Michael Borgwardt
thanks a lot! it sure is widely enough supported, but firefox and chrome support it thats more than enough for me
I__