Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selection change event in contenteditable

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() { ... });
like image 999
Yong Fei Avatar asked Dec 09 '11 07:12

Yong Fei


People also ask

What is select Change event?

The selectionchange event of the Selection API is fired when the current Selection of a Document is changed. This event is not cancelable and does not bubble. The event can be handled by adding an event listener for selectionchange or using the onselectionchange event handler.

How does Contenteditable work?

The contenteditable attribute specifies whether the content of an element is editable or not. Note: When the contenteditable attribute is not set on an element, the element will inherit it from its parent.

How do you use Contenteditable in Javascript?

You can add the contenteditable="true" HTML attribute to the element (a <div> for example) that you want to be editable. If you're anticipating a user to only update a word or two within a paragraph, then you could make a <p> itself editable.


3 Answers

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).

like image 175
Tim Down Avatar answered Sep 18 '22 16:09

Tim Down


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.

like image 44
mamoo Avatar answered Sep 19 '22 16:09

mamoo


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).

like image 43
dos Avatar answered Sep 17 '22 16:09

dos