On the front page of a site I am building, several <div>
s use the CSS :hover
pseudo-class to add a border when the mouse is over them. One of the <div>
s contains a <form>
which, using jQuery, will keep the border if an input within it has focus. This works perfectly except that IE6 does not support :hover
on any elements other than <a>
s. So, for this browser only we are using jQuery to mimic CSS :hover
using the $(#element).hover()
method. The only problem is, now that jQuery handles both the form focus()
and hover()
, when an input has focus then the user moves the mouse in and out, the border goes away.
I was thinking we could use some kind of conditional to stop this behavior. For instance, if we tested on mouse out if any of the inputs had focus, we could stop the border from going away. AFAIK, there is no :focus
selector in jQuery, so I'm not sure how to make this happen. Any ideas?
April 2015 Update
Since this question has been around a while, and some new conventions have come into play, I feel that I should mention the
.live
method has been depreciated.In its place, the
.on
method has now been introduced.Their documentation is quite useful in explaining how it works;
So, in order for you to target the 'input focused' event, you can use this in a script. Something like:
This is quite robust and even allows you to use the TAB key as well.
I'm not entirely sure what you're after but this sounds like it can be achieved by storing the state of the input elements (or the div?) as a variable:
As far as I know, you can't ask the browser if any input on the screen has focus, you have to set up some sort of focus tracking.
I usually have a variable called "noFocus" and set it to true. Then I add a focus event to all inputs that makes noFocus false. Then I add a blur event to all inputs that set noFocus back to true.
I have a MooTools class that handles this quite easily, I'm sure you could create a jquery plugin to do the same.
Once that's created, you could do check noFocus before doing any border swapping.