Update (5/14/2007): If you like this post, you might also appreciate validation hints.
Update (4/20/2007): Vox.com, the site whose registration form this article is based on, has changed that form, and it no longer uses the effect.
Update (4/3/2007): Fixed IE6 bug - see below.
Update (4/3/2007): textarea example provided
My co-workers pointed out a nice effect on the Vox registration form. As you tab through each input field, some helper text appears in box out to the right. Try it out. Update (4/20/2007): It has been pointed out that Vox has changed their form, and it no longer uses this effect.

It's a basic example of how helpful a little JavaScript and CSS can be in a form. Instead of the input hints always showing and potentionally cluttering a very simple form, only the hint for the currently focused input will show. This article will show a way to do this.
Go straight to the final example to see it in action, and to download a zip of the html, css, js, and images used.
The HTML
We'll start with a simple form. Vox's form used a definition list to mark up the form, and I'll be doing the same. I place the label in the <dt> tag, and the field in the <dd> tag, like so:
<dl>
<dt>
<label for="firstname">First Name:</label>
</dt>
<dd>
<input
name="firstname"
id="firstname"
type="text" />
</dd>
...
more <dt>'s and <dd>'s
...
</dl>
Immediately after each input field, add a span with a class of hint and create a hint for each input, like so:
<dl>
<dt>
<label for="firstname">First Name:</label>
</dt>
<dd>
<input
name="firstname"
id="firstname"
type="text" />
<span class="hint">Use your real name!</span>
</dd>
...
more <dt>'s and <dd>'s
...
</dl>
View what we have so far - an unstyled form.
Some CSS
Let's give this form some style. Assign our form-encompassing <dl> a fixed width and a position of relative. (This is important because our hints will be absolutely positioned relative to the containing <dl> rather than the <body>). Additionally, we'll style the <dt> and <dd> so that the form takes some shape.
dl {
position: relative;
width: 350px;
}
dt {
clear: both;
float:left;
width: 130px;
padding: 4px 0 2px 0;
text-align: left;
}
dd {
float: left;
width: 200px;
margin: 0 0 8px 0;
padding-left: 6px;
}
It's coming along. It's time to style the hints. Let's add the following to the CSS.
.hint {
position: absolute;
right: -250px;
width: 200px;
margin-top: -4px;
border: 1px solid #c93;
padding: 10px 12px;
background-color: #ffc;
}
See how it looks. All the hints are absolutely positioned, and their padding and containing content cause each of them to overlap a little. That's ok, because we'll only be showing one at a time. Add "display:none;" to the CSS.
.hint {
display:none;
position: absolute;
right: -250px;
width: 200px;
margin-top: -4px;
border: 1px solid #c93;
padding: 10px 12px;
background-color: #ffc;
}
Now that our form is styled and our hints are hidden, it's time for JavaScript.
The JavaScript
I am going to prepare the input fields so that as focus is put on them, their corresponding hint appears. I do this preparation on page load, using "addLoadEvent", because that's how I roll.
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
function prepareInputsForHints() {
var inputs = document.getElementsByTagName("input");
for (var i=0; i<inputs.length; i++){
inputs[i].onfocus = function () {
this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
}
inputs[i].onblur = function () {
this.parentNode.getElementsByTagName("span")[0].style.display = "none";
}
}
}
addLoadEvent(prepareInputsForHints);
The function prepareInputsForHints looks for all <input> tags and cycles through them. When there is focus on that input field, we grab the first span tag in the <dd> in which the <input> resides, and we change it's display to inline so that it'll show up.
To keep things simple, I've assumed that there is only one <span> tag next to each <input> tag.
What about <select>'s? We need to duplicate the logic we applied to the <input>'s so that it will work with <select>'s, too.
function prepareInputsForHints() {
var inputs = document.getElementsByTagName("input");
for (var i=0; i<inputs.length; i++){
inputs[i].onfocus = function () {
this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
}
inputs[i].onblur = function () {
this.parentNode.getElementsByTagName("span")[0].style.display = "none";
}
}
var selects = document.getElementsByTagName("select");
for (var k=0; k<selects.length; k++){
selects[k].onfocus = function () {
this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
}
selects[k].onblur = function () {
this.parentNode.getElementsByTagName("span")[0].style.display = "none";
}
}
}
addLoadEvent(prepareInputsForHints);
Check out what we have. It's functioning, for all inputs and selects, and it's looking pretty good. There are still a couple of things we can do to help it, though.
Minor Tweaking
Right now the hint is just in a box. Notice how in Vox's example, their hint box has a little arrow attached to it. Let's add that pointer in there. We want an image that has the same border and fill as the hint box, with the exception of the left border, which we don't want to exist. I'm going to use the same one Vox uses - a simple triangle.

This image will be added as a background image with the help of an additional span, which we need to insert within the hint in the HTML, and give a class of hint-pointer.
<span class="hint">Use your real name!
<span class="hint-pointer"> </span>
</span>
Let's style it so that it looks like it's part of the hint box.
.hint .hint-pointer {
position: absolute;
left: -10px;
top: 5px;
width: 10px;
height: 19px;
background: url(pointer.gif) left top no-repeat;
}
Also, up to this point, we've assumed that every <input> will have a corresponding hint. What if we don't need hints for some of those inputs? Who needs a hint for a field like "First Name"? We also don't want to show hints with submit buttons (remember, they are <input>'s, too!)? As of now, if there is a missing hint, a JavaScript error is returned.
There is more than one way to account for this. Here's one. Let's add some logic to the JavaScript that checks to see if a span exists before trying to change the style of that span.
function prepareInputsForHints() {
var inputs = document.getElementsByTagName("input");
for (var i=0; i<inputs.length; i++){
if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
inputs[i].onfocus = function () {
this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
}
inputs[i].onblur = function () {
this.parentNode.getElementsByTagName("span")[0].style.display = "none";
}
}
}
var selects = document.getElementsByTagName("select");
for (var k=0; k<selects.length; k++){
if (selects[k].parentNode.getElementsByTagName("span")[0]) {
selects[k].onfocus = function () {
this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
}
selects[k].onblur = function () {
this.parentNode.getElementsByTagName("span")[0].style.display = "none";
}
}
}
}
addLoadEvent(prepareInputsForHints);
Update (April 3, 2007)
It was pointed out that IE6 was showing some buggy behavior the first instance of showing the hints. The border wasn't completely being drawn around the span. I was going to pass it off as "buggy IE6", because I couldn't find a fix, but thanks to David Carriger in the comments, I think we found something.
In the .hint CSS, we have been giving span.hint a background color using "background-color:#ffc", but if we go ahead and give it a background image, too, IE6 will render span.hint properly.
.hint {
display:none;
position: absolute;
right: -250px;
width: 200px;
margin-top: -4px;
border: 1px solid #c93;
padding: 10px 12px;
background: #ffc url(pointer.gif) no-repeat -100px -100px;
}
Note that I gave it the same background image as the .hint-pointer span (less overhead that way), and positioned it with a negative margin so that it doesn't actually show up. This is now fixed in the final example and downloadable zip.
See final example, which only shows hints for username and password (and fixes the IE6 bug!).
I'd love to see other examples of similar effects. Let me know of any you like.
Comments (183)
That's a great article.
The end result is clean cut. I saw it on Vox already, but thanks for the "behind-the-scenes" look!
Posted March 23, 2007 1:42 PM | #
An interesting tweak on forms. Thanks for writing it up.
Why, though, wouldn't you just use the :focus pseudo-class?
Posted March 23, 2007 2:50 PM | #
@Charlie:
I can't envision how :focus would work here. The hint span is the element to show and hide, and there is no way in CSS for that to happen on the condition of another element's focus.
Posted March 23, 2007 2:56 PM | #
nice article there.
I see one problem here, what if a user has js disabled but not css? then the hints will always be invisible for such a user since they are hidden over css...
to fix this problem I would assign all styles which are used by the js-hints, e.g. display, position etc., over javascript, if you do it like that, the hints are only hidden if js is activated and will degrade nicely otherwise.
or you could assign a class to the form over js if you prefer having the styles in the css-file.
Posted March 26, 2007 2:24 AM | #
@CSS Guy:
It could be easily done using :focus and the adjacent sibling selector. Something along the lines of:
span { display: none; }
input:focus + span { display: inline; }
of course this will only work in browsers that understand the pseudo selector ':focus' and the adjacent sibling selector '+' (tested and works in FF2)
Posted March 27, 2007 6:28 AM | #
@Aaron,
I see now - :focus and adjacent sibling selector combo. Nice for the modern set of browsers.
Somewhere out there on the wide wide world of web is a countdown clock that tells us when we can start ignoring IE6. Anybody know what it currently says?
Posted March 27, 2007 7:37 AM | #
Knowing my luck it probably says:
Time remaining: NaN
And I could be wrong, infact I would love to be corrected on this...but I think IE7's support for selectors is pretty thin on the ground as well :(
Posted March 27, 2007 7:52 AM | #
Nice one! Beautiful interface in the front-end -- help when you need it most.
Posted March 27, 2007 9:22 AM | #
Very nice example. The only thing I'd do differently is give the SPAN tag and the INPUT/SELECT tags some common ID text and instead of looping through all the INPUT/SELECT elements on the page, just grab the one that matches up and display it:
<input
name="firstname"
id="firstname"
type="text"
onfocus="showHint(this.id);"
/>
<span class="hint" id="firstname_hint">Use your real name!</span>
document.getElementBy(element + "_hint").style.display = "inline";
That seems cleaner to me.
Posted March 27, 2007 9:51 AM | #
Why use definition lists (DL) for this? Hijacking markup that has a specific documented purpose for the convenience of your layout is a bad habit. The LABEL tag can be styled in place of DT and I'd rather see DIVs instead of DD.
I realize Vox set a bad example in that regard, but it might make sense not to propagate this sort of thing in a tutorial.
Thanks for the guide.
Posted March 27, 2007 9:52 AM | #
Wow, great example. Think I'm going to mess around with this tonight, but as Chris mentioned, using spans for the hints.
Posted March 27, 2007 10:02 AM | #
Also a great tweak would be to display the hints on mouseover :)
Posted March 27, 2007 10:35 AM | #
very cool, been looking for this sort of thing for a while now :)
Posted March 27, 2007 10:36 AM | #
I like this alot. Thanks for the free Java! (can't stand those damned curly brackets)...
Posted March 27, 2007 10:44 AM | #
Good job! It looks very nice and the tutorial is easy to follow. I really like this site.
Posted March 27, 2007 10:46 AM | #
I'm trying to find a way, like the VOX link, to keep the hint from hiding when I click on them. I want to be able to put hyperlinks in my hints. Any help?
Posted March 27, 2007 11:14 AM | #
For cleaner markup you could have used the alt="" attribute on the input for the copy and on focus use javascript to trigger that message in a window. That way you wouldn't have to make use of an unsemantic span.
Posted March 27, 2007 12:13 PM | #
to get hyperlinks to work, a little reworking needs to be done. Gone is the onblur event.
add input field type="hidden" id="lasthint"
then change the prepareInputsForHints() function to be structured like this:
Posted March 27, 2007 1:08 PM | #
For what it's worth - with the version you created, the hints only work in the final two fields on Firefox 2.0.0.3 on Mac OS X 10.4.9. However, the Vox example works fine for all fields. Unfortunately I am too much of an ignoramus to tell you why this is.
Posted March 27, 2007 3:41 PM | #
To save five lines of javascript you could change prepareInputsForHints() to -->
var formElements = ["input", "select"];
for(var g=0; g<formElements.length; g++){
var inputs = document.getElementsByTagName(formElements[g]);
for (var i=0; i<inputs.length; i++){
inputs[i].onfocus = function () {
this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
}
inputs[i].onblur = function () {
this.parentNode.getElementsByTagName("span")[0].style.display = "none";
}
}
}
Hopefully I didn't miss a parentheses. Coding in this textarea is a little tricky.
Posted March 27, 2007 3:55 PM | #
It cut off a portion of my post. You can create an array of form elements to look for input and select respectively and loop over that rather than coding the same thing twice. DRY.
Posted March 27, 2007 3:56 PM | #
Thanks for all the comments.
Let me see if I can respond to everyone.
@Gregg:
Nice approach with the onfocus="showHint(this.id);". I can see the appeal in that kind of organization.
@Chris regarding use of DL:
I disagree that using a DL to markup a form is wrong. I can't speak for why Vox chose to do so, but many times the needs of the client, visual appeal, and complexity of the form require markup in addition to form, fieldset, label, etc.. (In some cases, that even means using tables.) However, point taken about 'propogation'. So people reading this article: please note that just because this one form example is marked up with a DL, that doesn't mean you have to do it.
@PiratedTVPro:
Not Java. JavaScript.
@Matthew Davidson:
Good suggestion. I didn't consider that for this tutorial, but I appreciate you adding to it. I can see how that can be handy.
@web:
Yeah - that's another way to do it.
@Dan:
In my final example, the hints are purposefully only to show up for the last two fields. I think I stated that on that page. Let me know if I'm missing your point.
@Shakakai:
I'm no javascript guru, but I knew there would be a more efficient way. Thanks for sharing your code - I tried to modify your comment to match your intent. Remember to use ">" for ">", even in the javascript code snippets.
Posted March 27, 2007 4:29 PM | #
Thanks for the article; I will give it a try.
Posted March 27, 2007 7:10 PM | #
Why duplicate so much code? Couldn't you just create one array?
inputs = inputs.concat( document.getElementsByTagName("select") );
Posted March 27, 2007 9:12 PM | #
@Rob:
I knew there must've been some way to be more efficient, but I didn't know how to concatenate arrays in JavaScript (I'm not the JS Guy). Thanks for sharing - I'll try it out.
Posted March 27, 2007 9:16 PM | #
One could use :focus and the adjacent sibling selector and although that doesn't work for IE, of course, it also it doesn't work for Safari, although it does work on everything else.
One still could use a Conditional Comment for IE to add the Javascript needed to make it work in IE but that would still leave Safari out in the cold.
Posted March 28, 2007 12:44 AM | #
CSS Guy: I think Dan means that the hints do not show up nicely in IE. They show up, but it is like the disappearing text trick thing in IE where if you scroll the text suddenly shows up, same with your boxes that the hints are in. (maybe just on our set ups?)
Posted March 28, 2007 1:01 AM | #
I agree with Chris. Using DT and DD is absolutely no different from using table based layouts. Both are not designed for the purpose of laying out forms.
If you're going to (as Chris put it) hijack HTML, you may as well use the better suited table.
The example is great BTW, I just don't deem it appropriate that you promote the use of illegitimate HTML
Posted March 28, 2007 3:19 AM | #
This actually highlights a problem I was considereing the other day - is there a way to get rid of that span for the pointer? I like to keep my markup semantic.
My first thought involved removing the left border from the box, and including the border in the pointer image, but this would give you an overlap on the top and bottom borders, and doesn't allow for boxes of varying height.
Any ideas?
Posted March 28, 2007 11:57 AM | #
Cool idea. Gimmicky in most situations, though. =)
Posted March 28, 2007 1:33 PM | #
That is pretty slick. I now just have to re-arrange my page so that the "hints" are not over the top of anything...I would also like to play around to get them to match my site better so I have plenty to do...
----
Shredder
Posted March 28, 2007 1:52 PM | #
WOW!!!! Great stuff!!!! Thanks for sharing the tip!!!
Posted March 28, 2007 3:40 PM | #
Very nice article!
One thing that came to my mind when reading it is that it's unnecessary to add an extra div just for the pointer. Apply the pointer to the parent div instead (of course the image would need to be modified to fit this purpose; you'd have to add the border and some white yourself and make it about 400px high).
Posted March 29, 2007 4:09 AM | #
This is very cool.
What do you need to do in order to display different tips if there are two inputs on a single line?
Posted March 29, 2007 3:59 PM | #
This is a nice tutorial. I have implemented it fully, however I am having a problem when rendering in IE. It seems like IE does not draw the span correctly (borders are missing) the first time, but if the hint is shown and hidden again, it looks proper. Is there a way to force IE to redraw the screen? I also noticed that the Vox implementation does not have this problem, but their javascript is too cryptic for me!
Posted March 30, 2007 5:57 PM | #
Quote from Yeago:
"Gimmicky in most situations, though. =)"
Yeah, maybe it's gimmicky if you use it on obvious fields just for the sake of using it (like Vox).
But wonderful for offering hints w/o mucking up the initial view of the form with instructional text and in-line examples. It can mean the difference between making a form look simple vs. complicated and time consuming. Completed sign-up forms translates into dollars for most folks running an online business!
The "IA" Lives.
Posted March 30, 2007 6:09 PM | #
@All:
Some folks have pointed out an IE6 bug, in which the border around the hint does not draw completely around the span on its first viewing, but then looks fine.
I'm baffled. I've tried height:1%, zoom:1, etc. to get around this, but so far, nothing.
If anybody can find a way out, please share. Until then, please use with caution. One would think I could have a decent code walkthrough that is bug-free.
Also, as for using forms with <dl>, <dt>, and <dd>, though I have not used them until this very post, I'm still not convinced they are bad ideas. One could argue that the <dt> to <dd< relationship is a valid one to apply to a form field label and the field it represents.
For example: "First name" could be a term (<dt>). The use is asked to provide the definition (<dd>) via an input field.
Tell me again why this is wrong? I think that's a valid argument that a definition list is well-suited to handle forms if the designer so chooses.
Posted March 30, 2007 7:03 PM | #
A co-worker pointed me to this site to ask me what I thought of this form feature. I thought it was cool...uh...until I observed that it had that IE6 bug. Hah! And about 90% of our site visitors are still using IE6. So...I began playing with the coding.
This is what I came up with...and (at least on my local computer and development center for my network) appears to work for IE6/7/FF2. And don't ask me why, but by leaving out - what should and WOULD be considered proper - attributes for the classes used, things snapped to...AND...even MORE peculiar, I was forced (in my fix) to HAVE to place the "background-color" property directly in the "hint" SPAN...go figure.
In any case...it seems to work. Awesome job of coding CSS Guy!
CODING::: (core elements, only)
<style type="text/css">
/* The hint to Hide and Show */
.hint {
position: absolute;
display: none;
right: -250px;
width: 200px;
margin-top: -4px;
border: 1px solid #c93;
padding: 10px 12px;
color:#000000;
background-color: #ffc;
background: url(http://images.automart.com/imgs/design/ag/agonyx/autoblue/focused-left.gif);
background-position:-10px 5px;
background-repeat:no-repeat;
}
/* The pointer image is hadded by using another span */
.hint-pointer {
position: absolute;
left: -10px;
top: 5px;
width: 10px;
height: 19px;
background-color: #f2fcf1;
background: url(pointer.gif);
}
</style>
<script type="text/javascript">/* Not included in this submittal; however, no changes from original scripting. */</script>
<dl>
<dt>
<label for="firstname">First Name:</label>
</dt>
<dd>
<input
name="firstname"
id="firstname"
type="text" />
<span class="hint" style="background-color: #ffc;">This is the name your mama called you when you were little.<span class="hint-pointer"> </span></span>
</dd>
<dt class="button"> </dt>
<dd class="button">
<input
type="submit"
class="button"
value="Submit" />
</dd>
</dl>
...hope this was helpful.
Posted April 3, 2007 9:39 AM | #
@David,
I think you've hit on something! It turns out that including a background image as part of the .hint was all that was needed to get this working in IE6!
So if we go to .hint and change this:
background-color: #ffc;
to this:
background: #ffc url(pointer.gif) no-repeat -10px 5px;
it works!.
(Note that the background image is positioned so that it won't really show up. It's the same image as what would be in .hint-pointer span, so no overhead.)
David, I think you were having to explicitly state the background-color within the tag because, in the CSS, where you declare "background:", you didn't declare the color there. Since you were only declaring the background-image in that one statement, you would've probably gotten away with using "background-image" instead of just "background". When you use "background:", the color is expected, too.
Thanks for playing with this, David. I think we've got ourselves a fix. I'll need to update the article to reflect that.
Posted April 3, 2007 10:18 AM | #
Hey...was glad to help. Especially given that I have hopes of using this cool utility on my companies 2 sites. If you have no objections, that is. :-)
I'll let you know if & when my Product Development team approves my desired use for this tool, so's you can take a look-see.
Thanks for cleaning up my foo-pah coding. :-))
Best Regards, David
Posted April 3, 2007 10:28 AM | #
@David,
As far as I'm concerned, have at it - it's not my effect to give anyway. I just wanted to show how to achieve it.
Posted April 3, 2007 10:47 AM | #
Well done!!!
A question: how i can use a hint window in a textarea?
Thank you!
Posted April 3, 2007 4:31 PM | #
@gianluca:
See example 7 for a form with a textarea.
Posted April 3, 2007 5:23 PM | #
Nice writeup. I wonder if the Vox folks are wondering why so many folks are getting to their registration page and failing to sign up. They'll probably change their form or something ("I told you those javascript thingies were going to break folks' browsers!"). ;)
Posted April 3, 2007 6:43 PM | #
A coworker (that did the css on the regform) gave me the link to this post. I did the js on vox, btw. ;) Good writeup on the technique!
Cheers
David
Posted April 4, 2007 10:55 AM | #
great stuff.
really appreciated your understandable writing, and even though I didn't have a lot of knowledge about javascript, i got it ;)
Posted April 4, 2007 2:29 PM | #
Hi
I'm trying the last example which should have the IE6 fix, but still, using IE6, it does not mark up the whole border first time focusing on an input field.
http://www.askthecssguy.com/examples/formfieldhints/example06.html
Anyone who can confirm this?
Posted April 6, 2007 4:44 AM | #
@Martin:
I'm trying to get it again on two machines running windows with IE6, but it's looking ok here. What happens when you view example07.html?
Posted April 6, 2007 9:31 AM | #
How do I add 2 form field together. If I cheage the following from * to + I dont get the total but a combination of the 2 numbers
such as 2 + 3 = 23
var Total = Units * Rate
<html>
<script javascript>
<!-- Begin
//
// Server Side Code Would Be Required Above Script To Set Text Field Values From Database on OnLoad()
//
function DoMath(){
var Units = document.frmData.Units.value
var Rate = document.frmData.Rate.value
var Total = Units * Rate
document.frmData.Total.value = Total
}
<!-- End
</script>
<body onload="DoMath()">
<form name="frmData" method="POST" >
Units:<br>
<input type="text" onchange="DoMath()" name="Units" size="20"></p>
<p>Rate:<br>
<input type="text" onchange="DoMath()" name="Rate" size="20"></p>
<p>Total:<br>
<input type="text" name="Total" size="20"><br>
<input type="button" onclick="DoMath()" value="Update" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
</body>
</html>
Posted April 6, 2007 3:18 PM | #
@Steve:
Dunno. Try the sitepoint javascript forums.
Posted April 6, 2007 3:30 PM | #
@Steve:
you are trying to add strings, not numeric values. Take this code instead:
var Units = eval(window.document.frmData.Units.value);
var Rate = eval(window.document.frmData.Rate.value);
Posted April 12, 2007 5:35 AM | #
A nice understandable writeup!
Is there anywhere that the addloadevent code is explained. I know little JS and tend to come unstuck when trying to combine different techniques from different sources in one page.
I also would like to place links in the hint so thanks also to Matthew Davidson.
Posted April 15, 2007 4:01 AM | #
thanks for the interesting article.
what's wrong with using title-attributes on each input? they are accessible, don't need JS or CSS, and they work on mouseover.
too easy?
Posted April 15, 2007 12:33 PM | #
@Mark Snape:
Good point. "addLoadEvent" was written by Simon Willison, who can probably explain it better than I can. I learned about it when I read Jeremy Keith's "DOM Scripting", and have used it regularly since then to attach several scripts to the window.onload event.
@stefan:
The title attribute is always a good idea, and it has served as an easy way to get the tooltip effect. Howeveer, it doesn't quite achieve the same effect as the javascript-powered hints discussed here. (For one, they don't work when tabbing through the fields.) I encourage use of the title attribute anyway, so I'm with you, there.
Posted April 15, 2007 6:15 PM | #
Excellent tutorial, and some great feeback in the comments.
To stefan: I'd be tempted to use the title tag too, but in our experiments @ work, the title tag only allows a maximum of 255 characters in some browsers. Also, adding extra markup to the title tag (such as strong tags, paragraphs etc.) behaves oddly in older browers.
As to the popup technique as presented here, I'd be tempted not to use display: none; in the stylesheet so the tips appear by default when javascript isn't available. You could augment the script to add display: none; when it runs.
Posted April 16, 2007 3:47 AM | #
Very nice approach, but having to embed the <span class="hint-pointer"> </span> with each hint is not very designer-friendly. However, there is no reason why the JS that hooks up the events shouldn't at the same time inject the pointer span.
If you use the following as a replacement for your prepareInputsForHints() function, the hint-pointer spans will be added automagically. And the code will check for hints for input, select and textarea controls.
function prepareInputsForHints() { var tags = ['input', 'select', 'textarea' ]; for(var t = 0; t < tags.length; t++) { var inputs = document.getElementsByTagName(tags[t]); for (var i=0; i<inputs.length; i++){ connectInputHint(inputs[i]); } } } function connectInputHint(input) { var span = input.parentNode.getElementsByTagName("span")[0]; if(span) { // Add pointer var pointer = document.createElement('span'); pointer.className = 'hint-pointer'; pointer.innerHTML = ' '; span.appendChild(pointer); // Hook up events input.onfocus = function () { span.style.display = "inline"; } input.onblur = function () { span.style.display = "none"; } } }Posted April 16, 2007 4:23 PM | #
Good article :-)
The example (vox) seems to have removed the "Hints" :-) check it!
Posted April 20, 2007 4:19 AM | #
I love this example and I'm gonna use it but I still have the problem of getting it to work in IE6. If someone finds another soulution please post it here..Thanks..
Posted April 20, 2007 6:59 AM | #
@jigga,
Can you post a link to your form?
Posted April 20, 2007 8:51 AM | #
This is pretty cool, it'll be nice when ie 6 and other browsers that might not support this are no longer a sizable %.
Posted April 22, 2007 8:54 AM | #
I had to remove the "display: none;" from the CSS definition for .hint-pointer {} to get this to work. Might be a typo in your article.
Great script though.
Posted April 22, 2007 8:59 PM | #
Very useful! Thanks!
Posted April 23, 2007 2:33 AM | #
Its very cool. Keep posting more.
Posted April 23, 2007 5:27 PM | #
Very useful, thanks for the information. Love the site, very informative
Posted May 10, 2007 3:36 AM | #
First off, excellent article!
I'm running IE6 and still seem to have a display issue with the examples. The first on-blur event is displayed with some or no boarders, the next time its ok.
I did a little playing and it seams when i commented out the "position: absolute" from the css class ".hint-pointer", it solved the boarder display issue. Of course by doing this you lose the ability to position the pointer!
I did a bit of playing with the script and come up with this as a work-around:
I replaced the pointer span with a div and moved it to the beginning of the hint span.
I edited the ".hint" style so there was padding on all sides except the left.
Then used "position: relative;" in the ".hint-pointer" style to move the pointer.
Some people are probably going to curse at me for the way i have set this up, but hey......it works for me.
Can anyone confirm that they are having trouble with the original version but my work around is working for them?
A temporary example
Posted May 15, 2007 9:52 PM | #
for accessibility (well screen readers) should you not have the form hint before the input field?
this link has a nice example maybe someone can have a play and incorporate???
http://simplyaccessible.org/assets/formexample-errormessages.html
Posted May 16, 2007 11:55 AM | #
for accessibility (well screen readers) should you not have the form hint before the input field?
this link has a nice example maybe someone can have a play and incorporate???
http://simplyaccessible.org/assets/formexample-errormessages.html
Posted May 16, 2007 11:55 AM | #
thanks you
Posted May 17, 2007 4:34 PM | #
Hello! Great example! I've found it very useful. However. I was wondering if there was a way to make it show the hints when someone hovers their mouse over a table row?
Thanks!
Posted May 17, 2007 9:24 PM | #
Hi There! I was able to get this to work on mouseover with table rows using the following code:
var tableRow = document.getElementsByTagName("tr"); for (var m=0; < tableRow.length; m++){ if (tableRow[m].getElementsByTagName("span")[0]) { tableRow[m].onmouseover = function () { this.getElementsByTagName("span")[0].style.display = "inline"; } tableRow[m].onmouseout = function () { this.getElementsByTagName("span")[0].style.display = "none"; } } }You then just add the span with the hint in the LAST column of each row you want to have a hint in. You'll also have to make some minor changes to the CSS.
You''ll also note that I added an "if" statement. I would suggest adding this to all the other for loops as this will eliminate all the javascript errors that can occur if every row doesn't have a span tag.
For our implementation we also wanted to be able to dynamically turn the context help on or off. To do this I created a toggle function which depending on the current value of a cookie, toggled the code so that it set the EVENTs back to null.
Thanks for this great tutorial!
Posted May 25, 2007 8:56 AM | #
Hi There! I was able to get this to work on mouseover with table rows using the following code:
var tableRow = document.getElementsByTagName("tr"); for (var m=0; < tableRow.length; m++){ if (tableRow[m].getElementsByTagName("span")[0]) { tableRow[m].onmouseover = function () { this.getElementsByTagName("span")[0].style.display = "inline"; } tableRow[m].onmouseout = function () { this.getElementsByTagName("span")[0].style.display = "none"; } } }You then just add the span with the hint in the LAST column of each row you want to have a hint in. You'll also have to make some minor changes to the CSS.
You''ll also note that I added an "if" statement. I would suggest adding this to all the other for loops as this will eliminate all the javascript errors that can occur if every row doesn't have a span tag.
For our implementation we also wanted to be able to dynamically turn the context help on or off. To do this I created a toggle function which depending on the current value of a cookie, toggled the code so that it set the EVENTs back to null.
Thanks for this great tutorial!
Posted May 25, 2007 8:58 AM | #
@Cj B:
Nice stuff. Thanks for sharing how you customized the technique.
Posted May 25, 2007 9:24 AM | #
Too many comments to read through, but the article still has an error so I guess it hasn't been brought up. In the .hint .hint-pointer rule, you have display:none in there, but it shouldn't be. :)
Posted June 20, 2007 10:30 AM | #
@David:
Yikes! It's fixed. Thanks.
Posted June 20, 2007 11:19 AM | #
Good job! It looks very nice and the tutorial is easy to follow. I really like this site.
Posted July 12, 2007 12:43 PM | #
good text. thank yoouuu
Posted July 23, 2007 8:42 PM | #
thanks good text
Posted July 24, 2007 11:19 PM | #
Excellent article... now how can we kill IE6? It should have been dead a long time ago - but yet there it is causing us all kinds of headaches!
Posted July 27, 2007 8:11 PM | #
Hello CSSGuy,
thanks for the tip on the tooltip. ;)
I tried it and it works fine but W3c validation failed on javascript:
line 70 column 30 - Erreur: character ";" not allowed in attribute specification list
line 70 column 30 - Erreur: element "inputs.length" undefined
line 85 column 31 - Erreur: character ";" not allowed in attribute specification list
line 85 column 31 - Erreur: element "selects.length" undefined
line 97 column 8 - Erreur: end tag for "selects.length" omitted, but OMITTAG NO was specified
line 85 column 16 - Info: start tag was here
line 97 column 8 - Erreur: end tag for "inputs.length" omitted, but OMITTAG NO was specified
line 70 column 16 - Info: start tag was here
How can it be fixed (if it can)?
Posted August 3, 2007 12:38 AM | #
thanks good text