10

$(element)[0].selectionStart seems to only work for textareas. Is there an alternative for non textareas

I'm trying to wrap strings within DOM elements in tags (bold, italic...)

Also, how do I undo the wrapping?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Zebra
  • 3,858
  • 9
  • 39
  • 52
  • Take a look at this -- http://help.dottoro.com/ljqxhfte.php It looks like it might be a little tricky in IE though. – Mike Park Dec 15 '10 at 20:41

2 Answers2

9

This is a tricky area. You need the Selection object and DOM Range. However, IE does this all completely differently to proper browsers.

Wrapping the selection inside tags is also non-trivial in general: you need to consider all the text nodes within the selection.

My Rangy library normalizes the Selection/Range API for IE and has a module to apply a CSS class to a selection, which may help you to some degree. I'm also planning to write a more general module for applying other styles to a selection/range.

Sorry for the shameless self-promotion, but short of writing a lot of code yourself I don't know what else to suggest.

Tim Down
  • 318,141
  • 75
  • 454
  • 536
0

use the jQuery fieldSelection plugin http://plugins.jquery.com/project/fieldselection

i use it in my script that strips tags on paste and it seems to work great. http://blog.brokenbytes.info/2010/09/stripping-tags-on-paste-using-jquery/

m4tt1mus
  • 1,642
  • 14
  • 24
  • The fieldSelection plug-in is for textareas and inputs, which is precisely what the OP doesn't want. – Tim Down Dec 15 '10 at 23:35
  • A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](//stackoverflow.com/help/deleted-answers) – double-beep Apr 18 '19 at 19:02