6

My markup for contenteditable element is as below:

<iframe class="rich_text">
<html style="background:none transparent;">
    <head></head>
    <body contenteditable="true"></body>
</html>
</iframe>

Is there a selection change event for the body (contenteditable)? So that I can detect whether the selected text block has bold/underline etc.

I've tried some event handlers (jQuery) but without success:

var richText = $(".rich_text"),
richTextDoc = richText.contents()[0],
richTextBody = richText.contents().find("body");

// Enable Design mode.
richTextDoc.open();
richTextDoc.write("");
richTextDoc.close();
richTextDoc.designMode = "on";

// Binds selection change event
$(richTextDoc).bind("select", function() { ... });
$(richTextDoc).bind("selectstart", function() { ... });
richTextBody .bind("select", function() { ... });
richTextBody .bind("selectstart", function() { ... });
TylerH
  • 20,799
  • 66
  • 75
  • 101
Bananakilo
  • 1,323
  • 3
  • 14
  • 20

3 Answers3

6

Update 2017+

There is now a cross-browser way. Recent WebKit/Blink browsers (Chrome, Safari, Opera) support selectionchange, and Firefox supports it since version 43.

Old Answer

There is no cross-browser way of detecting changes to the selection. IE and recent WebKit browsers (Chrome and Safari, for example) support a selectionchange event on the document. Firefox and Opera have no such event and all you can do is detect selection changes made via keyboard and mouse events, which is unsatisfactory (there is no way of detecting "Select All" from context or edit menus, for example).

Sergey
  • 1,608
  • 1
  • 27
  • 40
Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • How sad :-( Any outlook in the future this will be fixed somehow ? Poor contenteditable ... – Frodik Jun 02 '12 at 07:21
  • 2
    @Frodik: There's an open W3C bug to spec it: https://www.w3.org/Bugs/Public/show_bug.cgi?id=13952. Things sometimes move somewhat if you make a fuss, although it's the browser implementers who have the most influence there. – Tim Down Jun 02 '12 at 11:16
5

Assuming that the content of your iframe is served from the same domain you could use:

$('.rich_text').contents()
  .find('body')
  .bind('selectstart', function(){});

As you can see from here, the selectstart event is correctly fired when the element is selected.

mamoo
  • 8,156
  • 2
  • 28
  • 38
  • "selectstarts" works :) , but on the document element only. The reason it doesn't work at first because I called `richTextDoc.open();` again later in the code, the event handler will not work if open() is called after that. Thanks :) http://jsfiddle.net/6e8cR/6/ – Bananakilo Dec 09 '11 at 07:49
  • 2
    This also does not work when the selection changes with the keyboard (scrolling the caret with the arrow keys. – marchaos Jan 25 '12 at 11:50
1

for firefox, https://developer.mozilla.org/zh-CN/docs/XPCOM_Interface_Reference/NsIEditor, offers OBSERVER on editors. Assumingly 'privileges' are needed as XPCOM-based. Other sol. on firefox beside mouse & kbd-tracking:

on 'focus' and 'blur' - events of all/concerning nodes/elements(text?) compare the node-content between state at focus-event and the node-content at the blur-event (= leave, also if window-close or sop'n similar), and set YOUR, or '_moz_dirty' , dirty-attribute(s. depending on whom/what browser it should serve, make also MANY different dirty-attrib's as purposes are requiring).

dos
  • 11
  • 1