Adding placeholders to inputs in Javascript
As I release my latest MooTools plugin, PlaceholderInput, that adds support for input placeholders, I decided to explain my solution, and what others are available.
An example of my implementation is seen on the Devthought sidebar, right next to this post. I’ll analyze next the different solutions that have been offered for this problem, and the upsides and downsides each has.
Solution #1: background-image
Upon the focus and blur events of the input, a background with the placeholder text is toggled.
Advantages
- The placeholder text is never submitted with the form
Disadvantages
- A change in the text, font, style or size means re-exporting the image
- The font rendering of the image and the actual text the user type will likely differ
- It becomes harder to support internationalization
Solution #2: overlapped element
A label or span element is positioned on top of the input. When the element is clicked, or the element is given focus, it disappears and the user can type normally.
Advantages
- The placeholder text is never submitted with the form
- Easy to change or translate the placeholder value, just by altering the markup
Disadvantages
- It’s very hard to position the text perfectly, in a cross browser fashion. Inputs should be padding less and border less to calculate the position perfectly, and styling becomes a problem.
I’ve seen this method in the new Yahoo! Mail interface search input. I tested with a couple of browsers and some displayed the text with a considerable offset, which depicts the aforementioned disadvantage.
Solution #3: altering the value
This is the most common solution, and in my opinion, the easiest to maintain and deal with its shortcomings.
Advantages
- The text will be rendered in the same position and style as the typed text
- Easy to change or translate the placeholder value, just by altering the markup
Disadvantages
- Unless you check the form before it’s submitted, the placeholder value will be sent. This is not a major issue with frameworks, because you can override the methods used to access values (like my solution)
The markup
The most straightforward way to implement solution #3 would be adding a placeholder attribute to the input like this:
<input type="text" placeholder="Your email" value="" name="email" id="contact_email" /> <textarea placeholder="Your message" name="message" id="contact_message"></textarea>
This certainly won’t validate, but you can always extend your Doctype. The inputs would be easily retrieved with a CSS selector by Javascript (input[placeholder]).
Another more accessible approach would be to transform labels into placeholders. Thus:
<label for="contact_email" class="placeholder">Your email</label> <input type="text" value="" name="email" id="contact_email" />
with some Javascript becomes
// we remove the label from the dom, or set it to display=none
<input type="text" value="" placeholder="Your email" name="email" id="contact_email" />and then the placeholder behavior is added normally.
MooTools implementation
Please head to the project page to see this in action.
-
sorry
i have to cut the brackets to get it up here:input class=”inputtest” type=”text” placeholder=”Your email” value=”" name=”email” id=”contact_email”
-
the last sentence was lost:
and: -
Hello,
you discribe a solution with an CSS selector by Javascript (input[placeholder]).
thats new for me.
I don’t understand this soloution for me it doesn’t work.
Have you a further eyapmle for me how thhe css looks like?
I did this without succcess:.inputtest input[placeholder]{
color: red;
font-size: 20px;}
and:thx for any answer
-
Мдааа… В некоторые моменты жизни волосы могут шевелиться и не только на голове
-
Hi guys,
My PC worked slowly, many mistakes and buggs. Please, help me to fix buggs on my computer.
I used Windows 2003.
Thx,
NonafurnNal -
Having created this type of script myself and having tried all three methods, I have to agree with you. Altering the value makes it so much easier to deal with the way it’s displayed.
I personally use the title attribute to place my placeholder text in – it’s faster to initialize and creates less code, but I do like the idea behind the label approach. Thanks for a great script!
-
The watermarked password field works quite well on https://www.bwin.com/
The nice with labels is that you can fade them in/out, which improves the interaction quite a bit.
I’ve got a experimental implementation for that here: http://jqueryjs.googlecode.com/svn/trunk/plugins/searchField/watermark-test.html
PS: The comment textarea is too wide, my text disappears on the right.
-
How about placing json data in the class? It would look somthing like:
And then you could parse the placeholder out as json to plug in the placeholder value. This should also validate too.
-
What about password fields? It’s not uncommon to have a login form at the top of a
page, where placeholders are useful. Solution #3 fails in that case.About solution #2: Libraries are quite good at calculting element offsets and gettingpaddings and borders shouldn’t be that hard either. Did you try to implement it?
Hi nto All
My PC worked not correctly, many errors. Help me, please to fix errors on my computer. Like a http://www.yahoo.com/ please.
My operation system is Windows Vista.
Thx,
HeniBoyhono