Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy formatting from the selected text?

Here's one usecase that has been baffling me. When I select some text and paste it in any of Google's applications like docs or notebook, it somehow manages to paste the text with its original formatting and sometimes images itself. Can someone tell me how to do this using javascript/jquery?

Edit: One more scenario that I am looking at is say designing a server-based copy/paste mechanism.

like image 958
Legend Avatar asked Feb 20 '11 03:02

Legend


People also ask

Which tool is used to copy formatting from a text to another?

The format painter lets you copy all of the formatting from one object and apply it to another one – think of it as copying and pasting for formatting. Select the text or graphic that has the formatting that you want to copy.

How do you copy paragraph formatting in Word?

Select the paragraph where you want to copy the formatting. On the Home tab, in the Clipboard group, click Copy Formatting. Select the paragraph(s) where you want to replace the formatting. On the Home tab, in the Clipboard group, click Paste, or use the keyboard shortcut Ctrl+V.


1 Answers

You can use contenteditable to do this:

<div contenteditable="true">Stuff pasted in here will retain their original HTML</div>

Here's a simple demo for this: http://jsfiddle.net/AA3Kq/


Regarding your second scenario, you'll want to look at JavaScript's selection API. I'm not an expert on this, but here's some places where you can get started on:

  • http://www.quirksmode.org/dom/range_intro.html
  • http://blogs.msdn.com/b/ie/archive/2010/05/11/dom-range.aspx
  • http://code.google.com/p/rangy/ and http://code.google.com/p/ierange/
like image 140
Yi Jiang Avatar answered Sep 23 '22 09:09

Yi Jiang