Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
deleted 68 characters in body
Source Link
Peeyush
  • 4.9k
  • 17
  • 67
  • 93

You can do this in this way:

use this jQuery plugin for pre & post paste events:

$.fn.pasteEvents = function( delay ) { if (delay == undefined) delay = 20; return $(this).each(function() { var $el = $(this); $el.on("paste", function() { $el.trigger("prepaste"); setTimeout(function() { $el.trigger("postpaste"); }, delay); }); }); }; 

Now you can use this plugin;:

var pasteSaveSel=[]; $('#txt').on("prepaste", function() { $(this).find("*").each(function(){ pasteSaveSelvar tmp=new Date.pushgetTime(); $(this).prop("tagName").toLowerCasedata()"uid",tmp); }); }).pasteEvents(); $('#txt').on("postpaste", function() {   var i=0; $(this).find("*").each(function(){   if(pasteSaveSel[i]!=$$(this).prop("tagName").toLowerCasedata("uid")){   $(this).removeAttrremoveClass("style");   }  $(this).removeAttr("style id");  i++;} }); }).pasteEvents(); 

Explaination

First saveset a uid for all the existing nodes tag name in an array PRE PASTE eventelements as data attribute.

Then after save compare all nodes name with the previous one in POST PASTE event. So by comparing you can identify the newly inserted one because they will have a uid, then just remove style/class/id attribute from newly created elements, so that you can keep your older formatting.

You can do this in this way:

use this jQuery plugin for pre & post paste events:

$.fn.pasteEvents = function( delay ) { if (delay == undefined) delay = 20; return $(this).each(function() { var $el = $(this); $el.on("paste", function() { $el.trigger("prepaste"); setTimeout(function() { $el.trigger("postpaste"); }, delay); }); }); }; 

Now you can use this plugin;:

var pasteSaveSel=[]; $('#txt').on("prepaste", function() { $(this).find("*").each(function(){ pasteSaveSel.push($(this).prop("tagName").toLowerCase()); }); }).pasteEvents(); $('#txt').on("postpaste", function() {   var i=0; $(this).find("*").each(function(){   if(pasteSaveSel[i]!=$(this).prop("tagName").toLowerCase()){   $(this).removeAttr("style");   }  i++; }); }).pasteEvents(); 

Explaination

First save all the existing nodes tag name in an array PRE PASTE event.

Then after save compare all nodes name with the previous one in POST PASTE event. So by comparing you can identify the newly inserted one, then just remove style attribute from newly created elements, so that you can keep your older formatting.

You can do this in this way:

use this jQuery plugin for pre & post paste events:

$.fn.pasteEvents = function( delay ) { if (delay == undefined) delay = 20; return $(this).each(function() { var $el = $(this); $el.on("paste", function() { $el.trigger("prepaste"); setTimeout(function() { $el.trigger("postpaste"); }, delay); }); }); }; 

Now you can use this plugin;:

$('#txt').on("prepaste", function() { $(this).find("*").each(function(){ var tmp=new Date.getTime(); $(this).data("uid",tmp); }); }).pasteEvents(); $('#txt').on("postpaste", function() { $(this).find("*").each(function(){ if(!$(this).data("uid")){ $(this).removeClass(); $(this).removeAttr("style id");  } }); }).pasteEvents(); 

Explaination

First set a uid for all existing elements as data attribute.

Then compare all nodes POST PASTE event. So by comparing you can identify the newly inserted one because they will have a uid, then just remove style/class/id attribute from newly created elements, so that you can keep your older formatting.

Source Link
Peeyush
  • 4.9k
  • 17
  • 67
  • 93

You can do this in this way:

use this jQuery plugin for pre & post paste events:

$.fn.pasteEvents = function( delay ) { if (delay == undefined) delay = 20; return $(this).each(function() { var $el = $(this); $el.on("paste", function() { $el.trigger("prepaste"); setTimeout(function() { $el.trigger("postpaste"); }, delay); }); }); }; 

Now you can use this plugin;:

var pasteSaveSel=[]; $('#txt').on("prepaste", function() { $(this).find("*").each(function(){ pasteSaveSel.push($(this).prop("tagName").toLowerCase()); }); }).pasteEvents(); $('#txt').on("postpaste", function() { var i=0; $(this).find("*").each(function(){ if(pasteSaveSel[i]!=$(this).prop("tagName").toLowerCase()){ $(this).removeAttr("style"); } i++; }); }).pasteEvents(); 

Explaination

First save all the existing nodes tag name in an array PRE PASTE event.

Then after save compare all nodes name with the previous one in POST PASTE event. So by comparing you can identify the newly inserted one, then just remove style attribute from newly created elements, so that you can keep your older formatting.