One of the new features coming in HTML5 is placeholder text for form inputs. The concept of placeholder text has been around for quite a while, and is fantastic for usability. Basically, placeholder text is text that appears inside of a form input when the page loads, but disappears when the user places the cursor in that form input.

Because HTML5 is still only partially supported, it’s still necessary to use javascript (you could conceivably use CSS to achieve most of the functions of placeholder, but most of the browsers that don’t support the HTML5 placeholder attribute also don’t support the necessary CSS to achieve this anyway). Within this article, I’m going to show you a few different options to add support for placeholder in all browsers.

To begin with, let’s take a look at how the placeholder attribute works. It’s quite simple. All you need to do is create an input that looks something like:

If you have a browser that supports the placeholder attribute, you’ll see “This is some placeholder text” in the form input whenever it’s empty. Of course, the placeholder attribute (at the time of this writing) is only supported in Chrome and Safari (no support for this attribute is currently available in Firefox, Opera or Internet Explorer), so you’ll need to do something else to get it working properly.

jQuery Solution

The easiest method is to use a jQuery plugin like placeHeld. PlaceHeld is a very nice, very easy-to-use plugin. Placeheld automatically determines whether or not the user’s browser supports the placeholder attribute. If the browser does not support it, the plugin fakes it by adding a value to the input that gets removed whenever the input receives focus. It even adds a CSS class to the input when it adds the placeholder text, so you can style your placeholder text differently from the standard input text. By default, a CSS class of “placeheld” is applied, but you can change that through the placeHeld options.

To use placeHeld, you will need to make sure that jQuery is in use on your site/page (if you’re using WordPress, it already is). If you’re not already using jQuery for other uses on your site, I would not recommend including it just for the use of this plug-in. If you are already using, it, though, this plug-in can be invaluable.

Once you’ve made sure jQuery is in place, you include the placeHeld plugin and then use some code similar to the following:

<script type="text/javascript">
/* Enable the use of the $ sign, even if jQuery is in compatibility mode
This method of invoking jQuery also makes sure the document is ready before
doing anything. */
jQuery(function( $ ) {
$( '#myinput').placeHeld();
});
</script>

That’s all there is to it. The placeHeld plugin will automatically detect the placeholder attribute within your input and use that text as the fake placeholder in browsers that do not support that attribute.

Pure Javascript Solution

If you are not currently using jQuery, you can use standard javascript to achieve something similar (though, it’s more difficult to get it not to execute in browsers that support the placeholder attribute, so this method will execute in all browsers, regardless of their support for placeholder). To do this, you would use some script similar to the following:

Did you like this post? Get monthly summary of our new tutorials, posts and tips to your inbox!

gen

Hi,
On my application, when i click on the placeholder text inside the textfield,the cursor does not appear but when I click at the end/outside the text ,the cursor appears .
{
div wairole=”presentation” id=”widget___o3.appsearch.searchText” class=”text dijit dijitReset dijitInline dijitLeft dijitTextBox” role=”presentation” widgetid=”__o3.appsearch.searchText”>

Enter the username
}
Double click on the text “Enter the username” works(can type anything inside textfield).
click on the end/outside the text works.(can type anything inside textfield).
But single click on the text and type does not work.(cannot see cursor at all)
What could be the problem here?
Thanks