SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have users copying and pasting content from a webpage into a SharePoint 2010 RTE. Unbeknownst to them, they are also copying styles that the RTE converts to span tags. Upon paste, I'd like to automatically remove any/all span tags but keep any remaining markup. I realize that there will be other related issues but removing the span tags will get me much closer to where I need to be.

I've found a code snippet here that removes all markup upon paste.

//Disable the RTE paste option. Restricts to "Paste Plain Text" 
function disableMarkupPasteForRTE()
 if (RTE)
  if(RTE.RichTextEditor != null)
   RTE.RichTextEditor.paste = function() { RTE.Cursor.paste(true); }
   // Handle Ctrl+V short cut options in rich text editor
   RTE.Cursor.$3C_0 = true;


Is anyone able to modify the above code to only remove span tags?

share|improve this question
up vote 1 down vote accepted

The SharePoint RTE is nothing more than a suped-up ContentEditable div. The solution is to bind a function onpaste to the editor and then manipulate the html directly.

$(document).ready(function() {
    $(".ms-rtestate-write").bind('paste', function(e) {
        var text = $(this).html();
        $(this).html(text.replace( /<\s*span.*?>/gi,"").replace( /<\s*\/\s*span\s*.*?>/gi,""));
share|improve this answer

You can use Sharepoint paste method and modify the html like this:

RTE.Cursor.$1G_0 = true;
RTE.Cursor.$ = '';
var $v_0 = RTE.Cursor.$S_0.contentWindow.document;
var $v_1 = $v_0.body;
var $v_2 = RTE.DomHelper.createRange($v_0);
var $v_3 = new RTE.Range($v_2);
$v_0.execCommand('paste', false, null);

html = $v_2.htmlText;
//modify the html source

//paste back your new html

var $v_4 = RTE.Cursor.get_range();
var $v_5 = $v_4.$1N();
RTE.Cursor.$ = 'none';
RTE.Cursor.$2U_0 = true;
RTE.Cursor.$28_0 = true;
window.setTimeout(RTE.Cursor.$9w, 0);
if (RTE.Cursor.$1u_0 > 0) {
    window.setTimeout(RTE.Cursor.$5p_0, 10);
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.