Use placeholder.
<input type="text" id="search" placeholder="Search..." />
Browser SupportChrome
Firefox 3.7+
Internet Explorer 9+
Avant Browser(Not sure)
Opera (Somewhere around v 11)
Safari 4+
Obviously not everybody is running a HTML5 browser, yet, so were going to need to fake it for the other, inferior browsers.
Add this to the head of each page, or add it to a separate script and call it on each page:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
// This adds 'placeholder' to the items listed in the jQuery .support object.
jQuery(function() {
jQuery.support.placeholder = false;
test = document.createElement('input');
if('placeholder' in test) jQuery.support.placeholder = true;
});
// This adds placeholder support to browsers that wouldn't otherwise support it.
$(function() {
if(!$.support.placeholder) {
var active = document.activeElement;
$(':text').focus(function () {
if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
$(this).val('').removeClass('hasPlaceholder');
}
}).blur(function () {
if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
}
});
$(':text').blur();
$(active).focus();
$('form:eq(0)').submit(function () {
$(':text.hasPlaceholder').val('');
});
}
});
</script>
<style>
.hasPlaceholder {
color: #777;
}
</style>
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
- <script>
- // This adds 'placeholder' to the items listed in the jQuery .support object.
- jQuery(function() {
- jQuery.support.placeholder = false;
- test = document.createElement('input');
- if('placeholder' in test) jQuery.support.placeholder = true;
- });
- // This adds placeholder support to browsers that wouldn't otherwise support it.
- $(function() {
- if(!$.support.placeholder) {
- var active = document.activeElement;
- $(':text').focus(function () {
- if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
- $(this).val('').removeClass('hasPlaceholder');
- }
- }).blur(function () {
- if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
- $(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
- }
- });
- $(':text').blur();
- $(active).focus();
- $('form:eq(0)').submit(function () {
- $(':text.hasPlaceholder').val('');
- });
- }
- });
- </script>
- <style>
- .hasPlaceholder {
- color: #777;
- }
- </style>
What the above code does, is check to see if the browser is compatible with placeholder. If it isn't, it mimics the effect using CSS. The color value inside .hasPlaceholder is the color of the faked placeholder text inside your text box. #777 is the default placeholder value.
Now every single browser that supports CSS can use this feature.A complete example:
<html>
<head>
<title>Placeholder Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
// This adds 'placeholder' to the items listed in the jQuery .support object.
jQuery(function() {
jQuery.support.placeholder = false;
test = document.createElement('input');
if('placeholder' in test) jQuery.support.placeholder = true;
});
// This adds placeholder support to browsers that wouldn't otherwise support it.
$(function() {
if(!$.support.placeholder) {
var active = document.activeElement;
$(':text').focus(function () {
if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
$(this).val('').removeClass('hasPlaceholder');
}
}).blur(function () {
if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
}
});
$(':text').blur();
$(active).focus();
$('form:eq(0)').submit(function () {
$(':text.hasPlaceholder').val('');
});
}
});
</script>
<style>
.hasPlaceholder {
color: #777;
}
</style>
</head>
<body>
<input type="text" name="search" placeholder="Search here..." />
</body>
</html>
- <html>
- <head>
- <title>Placeholder Example</title>
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
- <script>
- // This adds 'placeholder' to the items listed in the jQuery .support object.
- jQuery(function() {
- jQuery.support.placeholder = false;
- test = document.createElement('input');
- if('placeholder' in test) jQuery.support.placeholder = true;
- });
- // This adds placeholder support to browsers that wouldn't otherwise support it.
- $(function() {
- if(!$.support.placeholder) {
- var active = document.activeElement;
- $(':text').focus(function () {
- if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
- $(this).val('').removeClass('hasPlaceholder');
- }
- }).blur(function () {
- if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
- $(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
- }
- });
- $(':text').blur();
- $(active).focus();
- $('form:eq(0)').submit(function () {
- $(':text.hasPlaceholder').val('');
- });
- }
- });
- </script>
- <style>
- .hasPlaceholder {
- color: #777;
- }
- </style>
- </head>
- <body>
- <input type="text" name="search" placeholder="Search here..." />
- </body>
- </html>
Example:
LINKHope this helps, enjoy.
Every job is a self-portrait of the person who did it: Autograph your work with excellence.