Click here to Skip to main content
15,884,670 members
Articles / All Topics

JavaScript code to get selected text

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
30 Nov 2011CPOL 35.3K   1  
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.

JavaScript
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.

JavaScript
//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)}() 
JavaScript
//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)}()
JavaScript
//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)}()
This article was originally posted at http://blog.sabarinathanarthanari.com?p=109

License

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


Written By
Architect
India India
I have been programming for last 20 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

Comments and Discussions

 
-- There are no messages in this forum --