Re: How to change editable text on a fly, not loosing cursor position?

nel <[email protected]>
Newsgroups gmane.comp.mozilla.devel.editor
Organization http://groups.google.com
Message-ID <1184277273.376291.297110__15711.8205017925$1184277320$gmane$org@n2g2000hse.googlegroups.com>
Have you figured this out yet?  I'm still trying to make it work.
You can do a getSelection() to get the Selection object in Mozilla.
Then use http://developer.mozilla.org/en/docs/DOM:Selection to modify
and read the cursor position.

The problem is, the Selection object methods are sort of difficult to
work with, and i can't get it to work for the life of me.

-Najib

On Jun 19, 5:27 pm, juodas <[email protected]> wrote:
> Hello,
>
> I have an interesting (at least for me :) problem and I guess I'm trying
> to solve it somehow backwards, so it would be better to consult more
> experienced ones javascripters.
>
> I have to replace some text sequences in editable area (it may be
> editable div or iframes body with design mode on) on a fly.
> I mean if user types 'foo' it has to become 'bar' if user types 'f oo'
> and latter deletes space it should become 'bar', if user pastes text
> "foo was fooing on fooland" it has to become "bar was baring on
> barland". That is - on every text change no matter where and how much
> sequences of 'foo' appear, all of them have to become 'bar'.
>
> Problem source:
> STEP 1: after catching modification event (for example onkeyup), I get
> innerHTML of editable element
> STEP 2: and execute appropriate regexp on result
> STEP 3: and latter assign it back to editable lelements.innerHTML
>
> PROBLEM RESULT: i loose cursor position in text.
>
> I have tried to use selection object, but either used it in wrong way
> either the reason is in reassigning innerHTML (that causes recreation of
> editable elements child nodes and old stored selection becomes invalid).
>
> Question is: how correctly to do above mentioned 'foo' -> 'bar' replace
> without loosing cursors position, or how to bring cursor back where it
> had been before replacement of content to new one?
>
> Big thanks to everyone who will give any idea - as I'm knocking my head
> to the table and trying not to bite the neck (cord) of my mouse (to make
> it bleeeead!! argh... at least for the last free days. :)
>
> P.S. sorry if cross posting to javascript, editor & dom is a bit too
> much - just all these three seemed quite related to my question from
> different points.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.