Hi,
I have a Div that I want to make editable when clicking it or when tabbing into it if tabindex is set.
Logially I place a focus event handler on the the div as well as a click event listner triggering the focus as the focus event is not triggered when clicking a control that doesnt have a tabindex.
In the focus event handler I set editable='true' using attr().
Now when I click or by any other means (such as by code or tabbing) focus on the div, it becomes editable and I get a cursor in it. This goes for IE7, Chrome and my FF3.5 while not having a tabindex set.
As soon as I set a tabindex to the div FF suddenly triggers the same events and runs the same code but no cursor appears and I have to click an extra time to edit it (even though it has changed to editable='true') and still no cursor.
BUT! If I make a link run javascript to set focus to the Div. Everything works like a charm.
What mad events are triggered to set the focus inside the editable div?
If I set return false; inside the focus event handler after setting it to editable clicking still works but the link running javascript now fails to "focus for real" on the control. This makes me believe that there are more events getting triggered that I am missing. Which ones?
This is especially interesting as I was convinced that the focus event shot NOT bubble, ever, in any browser. Which in my head would mean that a return false would do no difference.
Also the focus event can be seen triggered (bubbling?) on the divs parent div if one exists as well as on the body and html tag.
What am I missing? I am going insane here. Please help me!!! :)
Regards,
Joel
---Code below
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
- <script type="text/javascript">
- function enable(){
- $('*').focus(function(){
- $('#textoutput').html($('#textoutput').html() + this.tagName + "." + this.className +".focus ");
- });
- $('*').click(function(){
- $('#textoutput').html($('#textoutput').html() + this.tagName + "." + this.className +".click ");
- });
-
- $('.textbox').focus(function(){
- $(this).attr('contentEditable', 'true');
- //return false;
- });
- $('.textbox').click(function(){
- $(this).focus();
- });
- $('.textbox').blur(function(){
- $(this).attr('contentEditable', 'false');
- });
- }
- $(function(){
- enable();
- });
- </script>
- </head>
- <body tabindex=-1>
- <div id='textoutput'></div>
- <div id='foo'>
- <div id="t2" class="textbox">
- text2
- </div>
- </div>
- <a href="javascript: var a=$('#t2').focus();">focus</a>
- </body>
- </html>