Forums

Unfortunately no one can be told what FluxBB is - you have to see it for yourself.

You are not logged in.

#1 2010-09-21 16:23:25

jojaba
Member
From: Obersoultzbach, Elsaß, France
Registered: 2009-12-04
Posts: 473
Website

[Javascript] How to extend a user selection ?

Hello smile

I tried to find by myself the solution to my problem since 3 days and can't solve it... I asked on a few forums on the Web, but no answer sad
I just discovered that there is a programming forum on FluxBB site, so i'll try it here wink
Here's what I need to know...
I'm developping the EZBBC Toolbar and had some feedback about a few bugs and requests. I fixed most of them but this one is really hard to find (this is due to my very basic knowledge in Javascript).
First of all, here's how the text selection of a user is handled:

if (window.ActiveXObject) { //For IE
                var textRange = document.selection.createRange();
                var currentSelection = textRange.text;
        } else { //For other browsers
                var startSelection   = field.value.substring(0, field.selectionStart);
                var currentSelection = field.value.substring(field.selectionStart, field.selectionEnd);
                var endSelection     = field.value.substring(field.selectionEnd);
        }

To avoid that smileys aren't take in account when the user forget the space between the text before or after it, I would like to extend the selected text to 1 character before and 1 character after the user selection. So that I can handle the omission of the spaces if needed. Here is what I made for the moment (it doesn't work of course):

if (window.ActiveXObject) { //For IE
                var textRange = document.selection.createRange();
                if (tagType == 'smiley') { //Change the textRange to include the previous and next character of the selection
                        textRange.moveStart('character', -1);
                        textRange.moveEnd('character', textRange.length + 1);
                        textRange.select();
                }
                var currentSelection = textRange.text;
        } else { //For other browsers
                var startSelection   = field.value.substring(0, field.selectionStart);
                var currentSelection = field.value.substring(field.selectionStart, field.selectionEnd);
                var endSelection     = field.value.substring(field.selectionEnd);
                 if (tagType == 'smiley') { //Change the textRange to include the previous and next character of the selection
                        currentSelection = field.setSelectionRange(startSelection.length-1, endSelection.length+1);
                 }
        }

I would really appreciate if I could have some help smile

Thanks in advance, Jojaba

Last edited by jojaba (2010-09-21 16:28:48)


Localize Mozilla extensions on BabelZilla

Offline

#2 2011-07-28 22:29:24

omibb
Member
Registered: 2011-07-06
Posts: 23

Re: [Javascript] How to extend a user selection ?

I found this, it might help, they specify 'textedit' rather than 'character' for the IE section:

http://www.java2s.com/Code/JavaScriptRe … xample.htm
http://www.java2s.com/Code/JavaScriptRe … xample.htm

Offline

#3 2011-09-04 03:15:47

iadamsky
Member
Registered: 2011-09-03
Posts: 4
Website

Re: [Javascript] How to extend a user selection ?

omibb wrote:

I found this, it might help, they specify 'textedit' rather than 'character' for the IE section:

http://www.java2s.com/Code/JavaScriptRe … xample.htm
http://www.java2s.com/Code/JavaScriptRe … xample.htm


thanks for this. smile

Offline

Board footer

Powered by FluxBB