How to remove each attribute from each tag?

I am trying to remove MS Word formatting on paste in Telerik RadEditer. Unfortunately, I cannot get the built-in formatting descriptor to work.

//performing paste var editor = $find("radEditor1"); editor.setFocus(); var rng = editor.getSelection().getRange(); rng.execCommand("Paste", null, false); //does nothing! (even when uncommented) //editor.fire("FormatStripper", {value: "MSWordRemoveAll" }); 

So, I suppose I can use jQuery to align all attributes from tags, and this can just do exactly what I need.

 //fixing content var html = editor.get_html(); $("*", html).each(function(){ var attr = $.map(this.attributes, function(item){ return item.name; }); var node = $(this); $.each(attr, function(i, item){ //added a filter for crazy Error if(item != "dataSrc" && item != "implementation" && item != "dataFld" && item != "dataFormatAs" && item != "nofocusrect" && item != "dateTime" && item != "cite") node.removeAttr(item); }); }); editor.set_html(html); 

Now after completing this function, my html variable does not update html ...

+4
source share
2 answers

I think I have.

 var html = editor.get_html(); var parent = $('<div></div>').html(html); $(parent).find('*').each(function(){ var attr = $.map(this.attributes, function(item){ return item.name; }); var tag = $(this); $.each(attr, function(i, item){ tag.removeAttr(item); }); }); editor.set_html(parent.html());​ 

http://jsfiddle.net/duC6Z/8/

Unfortunately, I still have problems with my RadEditor, but this is normal.

0
source

This code seems to do the trick. It uses the safeAttrs array to update the list of attributes you want to simplify. You can pass .removeAttr() list of attributes, separated by spaces, for deletion, so you don't have to loop through the attribute names one by one.

Finally, different browsers can handle attributes differently (for example, Chrome stores all attributes in lower case, so "dataFld" is stored as "datafld"), so it's best to normalize them with .toLowerCase()

 var safeAttrs = ["datasrc","implementation","datafld","dataformatas","nofocusrect","datetime","cite"]; $('html *').each(function() { var attrs = $.map(this.attributes,function(attr) { if($.inArray(attr.nodeName.toLowerCase(),safeAttrs) < 0) { return attr.nodeName; } else { return null; } }); $(this).removeAttr(attrs.join(' ')); }); 

jsFiddle DEMO . Use Chrome or Firebug to verify the items received to verify that the attributes have been removed.

+1
source

All Articles