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

JavaScript code to get selected text

By , 30 Nov 2011
 

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)

About the Author

Sabarinathan Arthanari
Technical Lead Mahindra Satyam
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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130617.1 | Last Updated 30 Nov 2011
Article Copyright 2011 by Sabarinathan Arthanari
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid