
Originally Posted by
yeomanjake
I am trying to find a script that does the following: Most anyone has been on Websites where a submission text box has text in it, like "Enter your Search String" or something like that, and as soon as you place the insertion point in the text field, the text disappears, thus allowing you to place your own string in the box. Does anyone know where I can find the script that lets you do this on your website?
Thanks!

In it's most basic form it can be done inline like so:
Code:
<input type="text" value="Please Enter Whatever" onfocus="if(this.value === this.defaultValue){this.value = '';}">
There are many variations, including those that restore the defaultValue if the text input loses focus and is blank.
Here's one way to do that for all text inputs on a page:
Code:
<!DOCTYPE html>
<html>
<head>
<title>Text Input defaultValue - Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<input type="text" value="Please Enter Whatever"><br>
<input type="text" value="Please Enter a Number"><br>
<input type="text" value="Please Enter a Color"><br>
<script type="text/javascript">
(function(){
var puts = document.getElementsByTagName('input');
for (var i = puts.length - 1; i > -1; --i){
if(puts[i].type === 'text'){
puts[i].onfocus = function(){if(this.value === this.defaultValue){this.value = '';}};
puts[i].onblur = function(){if(this.value === ''){this.value = this.defaultValue;}};
}
}
})();
</script>
</body>
</html>
Other variations strip white space from the values before evaluating them. There are a number of variations around both as to what is done and as to how the code goes about doing it.
Bookmarks