Click here to Skip to main content
Click here to Skip to main content

Tagged as

JavaScript code to get selected text

, 30 Nov 2011
Rate this:
Please Sign up or sign in to vote.
We know getting the selected text from a HTML page is a basic thing. But I think the code is not widely available.

We know getting the selected text from an HTML page is a basic thing. But I think the code is not widely available. The JavaScript code below fetches the value anywhere in a page including textarea, input and text fields of the forms.

function GetSelectedText()
{
  var selectedText=(
        window.getSelection
        ?
            window.getSelection()
        :
            document.getSelection
            ?
                document.getSelection()
            :
                document.selection.createRange().text
     );
 if(!selectedText || selectedText=="")
 {
    if(document.activeElement.selectionStart)
    {
     selectedText = document.activeElement.value.substring(
          document.activeElement.selectionStart
          . document.activeElement.selectionEnd);
    }
 }
 return selectedText;
}

Here are the bonus bookmarklets for the dictionaries for English and Tamil.

//English Dictionary

javascript:var%20newfunc=function%20a(){q=window.getSelection?
	window.getSelection():document.getSelection?document.getSelection():
	document.selection.createRange().text;if(!q||q=="")
	{if(document.activeElement.selectionStart)
	{q= document.activeElement.value.substring
	(document.activeElement.selectionStart,document.activeElement.selectionEnd)}}
	if(!q||q=="")q=prompt("Enter%20term:");if(q!=null)window.open
	("http://www.wordreference.com/definition/"+q)}() 
//English to Tamil

javascript:var%20newfunc=function%20a(){q=window.getSelection?window.
	getSelection():document.getSelection?document.getSelection():
	document.selection.createRange().text;if(!q||q=="")
	{if(document.activeElement.selectionStart)
	{q= document.activeElement.value.substring
	(document.activeElement.selectionStart,document.activeElement.selectionEnd)}}
	if(!q||q=="")q=prompt("Enter%20word:");if(q!=null)window.open
	('http://www.eudict.com/index.php?lang=engtam&word='+q)}()
//Tamil to English

javascript:var%20newfunc=function%20a(){q=window.getSelection?
	window.getSelection():document.getSelection?document.getSelection():
	document.selection.createRange().text;if(!q||q=="")
	{if(document.activeElement.selectionStart)
	{q= document.activeElement.value.substring
	(document.activeElement.selectionStart,document.activeElement.selectionEnd)}}
	if(!q||q=="")q=prompt('Enter%20word%20in%20Tamil:');
	if(q!=null)window.open('http://www.eudict.com/index.php?lang=tameng&word='+q)}()

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sabarinathan A
Technical Lead Tech Mahindra
India India
I have been programming for last 10 years on various platforms including .NET, Visual Basic 6, Oracle and SQL server.
 
I decided to write articles when I have free time hoping to share my thoughts with you.
 
To know more about me visit http://sabarinathanarthanari.com
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 30 Nov 2011
Article Copyright 2011 by Sabarinathan A
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid