PDA

View Full Version : Using focus() to hide div?


MagSafe
2007-10-31, 06:45
Hi again,

I have another query related to JavaScript :)

Basically I have a div that drops-down when the user clicks a button, it's originally set to "display: none;" so when the user clicks the button its then set to "display: block;", and vice versa.

What i'd like to do is make this similar to drop-down window's in applications, where if the window is shown and the user clicks somewhere else on the screen, the window is then hidden again.

Is there a way of doing this with JavaScript? I'm thinking of using focus() to set the div to "display: none;" again when the user clicks somewhere else on the screen, but i'm not sure on how to go about doing this!

Cheers for any help,

Steve.

ast3r3x
2007-10-31, 07:00
You mean like using onFocus() and onBlur()?

MagSafe
2007-10-31, 07:51
Yeah, thats what I mean :)

Except the opposite if you know what I mean, so when it isn't focused it'll do something.

Thanks :)

ast3r3x
2007-10-31, 08:17
Can't you do something likeā€¦

<element onBlur="do-whatever-you-want">asdfsdfasdf</element>

Shouldn't that do whatever you do when you are no longer focused on an element?

MagSafe
2007-10-31, 08:37
Just tried that,

It doesn't seem to working, I checked on W3 Schools and the div object isn't supported unfortunately.

Thanks anyway though :)

Gargoyle
2007-10-31, 09:55
If the button that they click on is within the div that drops down, you could use onmouseout="".

MagSafe
2007-10-31, 11:46
I got it working ...eventually, it took about 2hr's but I finally worked it out :)

What I did was use the onclick event to see if the div was shown, if it was and the user wasn't clicking within the div, the div would be hidden.

If anyone wants the code, let me know :)

Thanks again,

Steve.

ast3r3x
2007-10-31, 14:27
Ohhh, I get what you wanted to do now. For some reason I re-read your post at least 3 times and couldn't figure out exactly what you were trying to do, which is why I used the <element> because I didn't know what you were applying it to. Of course reading your original post now is pretty clear. Well anyways good job, sorry I would have helped if I wasn't retarded. :p

MagSafe
2007-10-31, 14:31
Ohhh, I get what you wanted to do now. For some reason I re-read your post at least 3 times and couldn't figure out exactly what you were trying to do, which is why I used the <element> because I didn't know what you were applying it to. Of course reading your original post now is pretty clear. Well anyways good job, sorry I would have helped if I wasn't retarded. :p

Yeah it's quite complicated to explain :p

Thanks for the help though, you at least pointed me in the right path :)