selectionStart for non textarea elements
Asked Answered
N

2

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?

Nunhood answered 15/12, 2010 at 19:10 Comment(1)
Take a look at this -- help.dottoro.com/ljqxhfte.php It looks like it might be a little tricky in IE though.Bravo
I
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.

Interconnect answered 15/12, 2010 at 21:45 Comment(0)
B
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/

Bonkers answered 15/12, 2010 at 22:17 Comment(2)
The fieldSelection plug-in is for textareas and inputs, which is precisely what the OP doesn't want.Interconnect
A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link 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.Repudiation

© 2022 - 2024 — McMap. All rights reserved.