The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Help needed with a getSelection Javascript
denmarks
post Oct 24 2013, 11:10 AM
Post #1


Advanced Member
****

Group: Members
Posts: 207
Joined: 17-January 08
Member No.: 4,734



I have a problem with the following Javascript which is used in a bookmarklet. I can select most text on any screen and run the bookmarklet and the text is displayed. When used to select text within an input text box it only works in Yahoo Mail. This may somehow be related to Yahoo constantly storing the text in a Drafts file. Is there a way to get it to work everywhere? For example if I highlight text as I am typing here it displays a blank. If I go to the top of the screen and select "Client-side Scripting" it displays it since it is not within this box. I need the getRangeAt portion in order to replace text in a full version of the script. NOTE: javascript gets split into java script in the code display below.

CODE
java script:inputString=document.getSelection().getRangeAt(0);alert(inputString);void(0);
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
denmarks
post Oct 24 2013, 01:44 PM
Post #2


Advanced Member
****

Group: Members
Posts: 207
Joined: 17-January 08
Member No.: 4,734



The more I read the more I see it is not a simple matter when extracting from a textarea. I wonder why it works in Yahoo Mail. I guess they are not using a textarea.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Oct 24 2013, 03:24 PM
Post #3


.
********

Group: WDG Moderators
Posts: 9,648
Joined: 10-August 06
Member No.: 7



This should work in newer browsers including IE9:

CODE
var textarea=document.getElementById('textarea');
if(textarea.selectionStart>=0)
{
    var text_before_selection=textarea.value.substring(0, textarea.selectionStart);
    var text_after_selection=textarea.value.substring(textarea.selectionEnd);
    var text_selection=textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
}

For IE8 and older other tricks are necessary.
User is online!PM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 16th April 2024 - 10:46 AM