Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove tags from selection

I've got the following code:

<div contenteditable="true" id="editor">
    <p>This is example text with <span class="spoiler">spoiler<strong>s</strong></span></p>
  <p>The <span class="spoiler">spoiler</span> exists in multiple paragraphs</p>
</div>
<button onclick="removeSpoiler();">remove spoiler</button>

The user can select text and after that click on the button to remove the <span class="spoiler"> formatting. After clicking the button, the text must be still selected.

For example: The user selects "with spoilers. The sp". He clicks on 'remove spoiler'. The desired output is:

<div contenteditable="true" id="editor">
    <p>This is example text with spoiler<strong>s</strong></p>
  <p>The sp<span class="spoiler">oiler</span> exists in multiple paragraphs</p>
</div>
<button onclick="removeSpoiler();">remove spoiler</button>

A jsFiddle of my attempt (I really don't know where to go from there): http://jsfiddle.net/632cr/

like image 929
klaasjansen Avatar asked Jan 18 '14 21:01

klaasjansen


People also ask

How do I remove a tag from my data?

From the pop-up menu that displays after you click the tag, select Clear tag. Alternatively, you can find the data in the My Data Items column, rest the mouse pointer on the data, and click the X that appears.

How do I delete a tag from Libby?

Deleting a tag permanently removes it from Libby. Deleted tags can't be recovered. Go to . Tap tags at the top of the screen. Tap the tag you'd like to delete. Tap Actions. Tap Delete Tag, then Yes, Delete Tag.

How do I clear tags in structured data markup?

From the pop-up menu that displays after you click the tag, select Clear tag. Alternatively, you can find the data in the My Data Items column, rest the mouse pointer on the data, and click the X that appears. You can clear all of the tags Structured Data Markup Helper has created, but be careful: you cannot undo a clear all tags request.


1 Answers

The fastest and easiest way to do this is to use rangy framework and its CSSClassApplier module.

It's easy and your code could look like this:

rangy.init();

var cssClassApplierModule = rangy.modules.CssClassApplier;
var classApplier = rangy.createCssClassApplier('spoiler');

function removeSpoiler(){
    classApplier.undoToSelection(editor);

    // it's some preview div
    $('#preview').text( $(editor).html() );
}

See the result demo here.

like image 150
Tony Avatar answered Sep 28 '22 04:09

Tony