![]() |
|
|
|||||||
![]() |
|
|
Thread Tools | Rate Thread |
|
|
PM User | #1 |
|
New to the CF scene Join Date: Feb 2008
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
![]() |
onfocus and onblur on a div with Firefox
I want to do something like..
Code:
<div onfocus="someFunc();">foo</div> Code:
document.getElementById('myDiv').addEventListener('focus', someFunc, false);
Any ideas how I can get any kind of focus/blur action for divs working in Firefox? Thanks! |
|
|
|
|
|
PM User | #3 |
|
Red Devil Mod ![]() ![]() Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 6,842
Thanks: 49
Thanked 219 Times in 215 Posts
![]() ![]() |
I guess you may capture the click event and see if the target is or not a certain div. A basic example:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<style type="text/css">
#mydiv{
width:100px;
height:100px;
background:#f00;
}
</style>
<script type="text/javascript">
function whichDiv(e){
var div=document.getElementById('mydiv');
var target=e?e.target:event.srcElement;
var mess=target==div?'you focused the "mydiv" DIV':'you blured the "mydiv" DIV';
document.getElementById('mytext').innerHTML=mess;
}
document.onclick=whichDiv
</script>
</head>
<body>
<div id="mydiv">my div</div>
<div id="mytext"></div>
</body>
</html>
|
|
|
|
|
|
PM User | #4 |
|
Regular Coder ![]() Join Date: Jan 2008
Location: Milwaukee, WI
Posts: 724
Thanks: 8
Thanked 96 Times in 95 Posts
![]() |
Try
to hit the event in the capture phase instead of the bubbling phase, this might give the effect you're looking for. But since Divs aren't like certain other html or ui controls that can activate, focus doesn't really apply. Things like the div don't focus because they don't maintain a cursor position or a special state (natively) when you click on it (or if you could -- tab to it). You could be considering maybe that hovering is synonymous with focusing (you could implement this with onmouseover & onmouseout)? If you're actually getting a behavior out of ie with onfocus/onblur, it's probably "quirks mode" and you should look for a more appropriate set of handlers to service your needs.
__________________
-Mike "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender |
|
|
|
|
|
PM User | #5 | ||||
|
New to the CF scene Join Date: Feb 2008
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
![]() |
Quote:
Quote:
Quote:
Quote:
|
||||
|
|
|
|
|
PM User | #6 |
|
Red Devil Mod ![]() ![]() Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 6,842
Thanks: 49
Thanked 219 Times in 215 Posts
![]() ![]() |
A note: if the DOCTYPE is XHTML, you should isolate the embedded javascript codes in CDATA islands (to avoid XML misinterpretation)
Code:
<script type="text/javascript"> /*<![CDATA[*/ ..... code here ... /*]]>*/ </script> |
|
|
|
|
|
PM User | #7 | |
|
Regular Coder ![]() Join Date: Jan 2008
Location: Milwaukee, WI
Posts: 724
Thanks: 8
Thanked 96 Times in 95 Posts
![]() |
Quote:
__________________
-Mike "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender |
|
|
|
|
|
|
PM User | #8 |
|
Red Devil Mod ![]() ![]() Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 6,842
Thanks: 49
Thanked 219 Times in 215 Posts
![]() ![]() |
Sir, this (addEventListener() method) works on every browser, except IE, who uses attachEvent(). You should have learned that by now. Moreover, the entire way IE handles the events is different from the DOM model. This is it.
Last edited by Kor; 03-08-2008 at 04:19 PM.. |
|
|
|
|
|
PM User | #9 |
|
Regular Coder ![]() Join Date: Jan 2008
Location: Milwaukee, WI
Posts: 724
Thanks: 8
Thanked 96 Times in 95 Posts
![]() |
just checking, but are you saying i'm right Kor or that i've done something wrong?
__________________
-Mike "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender |
|
|
|
|
|
PM User | #10 |
|
Red Devil Mod ![]() ![]() Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 6,842
Thanks: 49
Thanked 219 Times in 215 Posts
![]() ![]() |
I am just saying that you have posted a solution which is not crossbrowser. Like it or not, IE has, so far, over 2/3 of the market, so that we have to know the differences between IE and the others (Moz, Opera, Safari...). My post on #3 takes care of that.
Last edited by Kor; 03-08-2008 at 06:18 PM.. |
|
|
|
|
|
PM User | #11 |
|
Regular Coder ![]() Join Date: Jan 2008
Location: Milwaukee, WI
Posts: 724
Thanks: 8
Thanked 96 Times in 95 Posts
![]() |
i apologize, i was indicating only a fix for the problem selected and had intended the solution to be "switched" (obj.attachEvent?obj.attachEvent(...)
bj.addEventListener(...)) but i did not state it as such for some reason, good call
__________________
-Mike "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender |
|
|
|
|
|
PM User | #15 | |
|
New to the CF scene Join Date: Mar 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
Quote:
Here is an interesting article: http://snook.ca/archives/accessibili...with_tabindex/ |
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Rate This Thread | |
|
|