Ajeesh M
Ajeesh M

Reputation: 2092

Losing selected text from contenteditable on clicking div with css user-select: none;

I'm developing a JavaScript rich text editor. Instead of buttons, I'm using div with css property user-select: none. This works fine in Firefox but problem is with Google Chrome.

When I click on div with css user-select: none, I'm losing the selected text from contenteditable div. Html and css is given below

<div id="editor">
   <div id="controls">
      <div id="button-bold"></div>
   </div>
   <div id="rte" contenteditable></div>
</div>

CSS

#button-bold
{
    width: 20px;
    height: 20px;
    padding: 2px;
    float: left;
    border: 1px solid #E7E7E5;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
#rte {
    width: 100%;
    min-height: 400px;
    margin: 5px;
    outline: none;
}

Upvotes: 1

Views: 1587

Answers (2)

Dharmendra Jadon
Dharmendra Jadon

Reputation: 141

Use buttons, intead of div or span for making clickable links to change style.

And then use document.execcommand('forecolor',false,'#000000');

Upvotes: 1

Yoshi
Yoshi

Reputation: 3405

I have a similar setup, and the following solution works fine in Chrome: How to disable text selection using jQuery?

The answer is to add a handler for the 'selectstart' event.

Upvotes: 0

Related Questions