This is possible with pure CSS, if you can change your markup a bit.
div { background: #f0a; display: none; height: 200px; width: 200px; } input:focus + div { display: block; }
What I essentially do is to hide the div first, and when the input field has focus, the next next match with the div changes its display to block .
For this to work, it must be the next next brother, since you cannot travel the DOM tree, so you need to expand your input field. from span .
See my fiddle: http://jsfiddle.net/TheNix/U28sd/
EDIT:
The adjacent selector ( + ) should work in IE7 and higher (although I think there may be some problems with asynchronously loaded content). Note that the item should appear immediately after that, so itβs just not the βnext item that matches Yβ, but actually the βitem next to X if it matches Yβ.
Several times, if you know the markup and want to hack, you can "count" your way down. input + div + div will target the second div , for example (assuming the markup matches exactly). Of course, I would not recommend it, as it will explode at the slightest change in your layout and it will be painful to support.
Read more about selectors here: http://www.w3.org/TR/CSS2/selector.html#adjacent-selectors
source share