?
Solved

focusNode is being taken away by a click outside a contenteditable div

Posted on 2013-06-03
4
Medium Priority
?
1,027 Views
Last Modified: 2013-06-05
I have a div where I've set contenteditable=true. After selecting some text in the div I'm able to execute things like document.execCommand('bold', false, false); and get the desired result.

But when I try this with document.execCommand('forecolor', false, color); nothing happens and I can see that the reason is that when I go off to my color picker to click a color box, : window.getSelection() shows on return that focusNode is now the color box .

I've done a jQuery .focus() on the original text div but that didn't bring focusNode back to the selected text. Could someone point out where I'm going wrong?  It seems that if you click on something that doesn't have contenteditable=true, like my color box, it shouldn't move focusNode.   I'm working with the FireFox browser.

Thanks for any suggestions.

Thanks.
0
Comment
Question by:steva
  • 2
  • 2
4 Comments
 
LVL 35

Expert Comment

by:Slick812
ID: 39219374
greetings   steva, , depending on how the "color picker" is displayed, you may have to kill (not show) the "color picker" display AFTER you get the color value from it. And then try to get focus back to the editable div.
Also, I have found that sometimes javascript code sequences can not be handled in the instant of time of the code lines (the DOM changes are not always instant, or the javascript code line continues even though the DOM has NOT completed the task), and you might try to use a delay to reset the focus using setTimeout( ) with 100 millisecond delay after you hide (close) the color picker.
0
 

Author Comment

by:steva
ID: 39219929
Hi slick. Thanks for responding. I've collected more information since I posted.  It has nothing to do with an element having contenteditable.  If you select text on any web page the browser highlights it  and then if you click somewhere else on the page the text you originally selected becomes un-highlighted,   So what I'm seeing  is normal behavior. You can prevent losing the focus when you click somewhere else by not letting that mousedown event propagate.So if you click on something else but don't let the event propagate up to where it steals the focus, you're good.  That's what the links for Bold, etc. did, by ending with a return false. Or if you click on something that's draggable, you don't take the focus away, and I believe this is because UI draggable cancels its mousedown event.

But there are still things here that are not clear to me. You have cancel the event with return false. event.stopPropagation() doesn't do it.  And you have to catch mousedown and cancel it. Catching click and canceling it doesn't work.: you lose focus.

So if you have some experience in this area, any light you (or anyone else) can shed would be much appreciated.

Thanks
0
 
LVL 35

Accepted Solution

by:
Slick812 earned 2000 total points
ID: 39222779
OK, from your beginning statement I failed to catch what you were doing (trying to do), and now I see your problem, I have done some in javascript for selecting and changing text (usually in a "textarea"), once you loose focus, you also loose Any and All for a reference to any highlighted text and can not go back to selected with system call,
So what I remember doing, was to get the beginning and end position of the selected text and store them in a global variable (array), before you change the focus, to another element to get text value or color value, and then when you have data for change get the TEXT element, and call system functions to RE-SELECT the text length, and then change the "selected" text or it's properties (color). There are thousands of web pages that can show you how to get the "selected start" position and selected length, and how to insert or change the selected, , I think I remember using  the JS "document.selection". I may can find some code for this, but it is a well documented tutorial thing for JS.
0
 

Author Closing Comment

by:steva
ID: 39223253
Ok Slick.  Thanks for your help.  I gave you the points.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When I'm searching for answers on Experts Exchange, I often use Google because it's built into my browser.  To search only on Experts Exchange, I use the "site:" search operator, which can be cumbersome to type out each time I want to run a search.…
When you put your credit card number into a website for an online transaction, surely you know to look for signs of a secure website such as the padlock icon in the web browser or the green address bar.  This is one way to protect yourself from oth…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Google currently has a new report that is in beta and coming soon to Webmaster Tool accounts. This Micro Tutorial will highlight new features for Google Webmaster Tools.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question