Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 30th, 2006, 08:18 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 198
Thanks: 2
Thanked 0 Times in 0 Posts
Send a message via MSN to itHighway
Default Selected Text

Hello,

I need some help with javascript. I have developed a page with some Text on it. What I need is when someone selects any part of the text using mouse and release the mouse a popup open and display the selected text.

Exmaple:
"This is Wrox Forum"

If I select 'Wrox Forum', on releasing mouse key an alert box open displaying selected text i.e 'Wrox Forum'.

Any idea..
Any help..

Will really be appriciated.

Thanks

 
Old January 30th, 2006, 09:33 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello again itHighway,

Try this:

<p id = "p1" onmouseup = "grabText()">Some text to select.</p>

<script language = "javascript">

function grabText(){
var theText = document.selection
        if(theText.type =='Text')
        {
        var newText = theText.createRange();
        alert(newText.text)
        }

}

</script>

HTH
Joe

 
Old January 30th, 2006, 02:14 PM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 198
Thanks: 2
Thanked 0 Times in 0 Posts
Send a message via MSN to itHighway
Default

Thank you very much. It worked fine.

Is it possible to disable a word from selection. Mean user not able to select any word or phrase in a string.

Example:
"This is Wrox Forum"

User can select "This is" and "Forum" but cannot select "Wrox".

Regards,
ZA

 
Old January 31st, 2006, 09:52 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi itHighway,

Yes its possible. Just set a variable in the javascript to define the word or words you wish to prevent selection of. You could even have an array of words if you wanted to prevent selection of multiple different words. Here's the script for one word:


<script language = "javascript">

function grabText(){

var denySelection = "wrox"
var theText = document.selection

        if(theText.type =='Text')
        {
        var newText = theText.createRange();
        }
        if (newText.text == denySelection){
        document.selection.empty()
        }
        else{
        alert(newText.text)
        }

}

</script>


And this is a script that checks for selections across an array, using a for loop:

<script language = "javascript">

function grabText(){

var denySelection = new Array()
var theText = document.selection

denySelection[0] = "Welcome"
denySelection[1] = "to"
denySelection[2] = "wrox"



        if(theText.type =='Text')
        {
        var newText = theText.createRange();
        }

        for(i =0; i<=2;i++)
        {
            if (newText.text == denySelection[i])
            {
            document.selection.empty()
            }
        }


}

</script>

HTH

Cheers
Joe

 
Old February 2nd, 2006, 12:04 PM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

is it possible to have your entire document unselectable?

www.crmpicco.co.uk
 
Old February 3rd, 2006, 06:18 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Picco,

This should do it:

<script language="JavaScript1.2">

function disabletext(e){
return false
}

function reEnable(){
return true
}

//if the browser is IE4+
document.onselectstart=new Function ("return false")

//if the browser is NS6
if (window.sidebar){
document.onmousedown=disabletext
document.onclick=reEnable
}
</script>


HTH
Joe






Similar Threads
Thread Thread Starter Forum Replies Last Post
how to get text of a particular cell of selected r jjkk BOOK: ASP.NET AJAX Programmer's Reference : with ASP.NET 2.0 or ASP.NET 3.5 ISBN: 978-0-470-10998-4 0 January 22nd, 2008 07:39 AM
change the color of selected text dynamically NEO1976 Flash (all versions) 1 December 18th, 2007 11:11 AM
How to retrieve last selected text line in text bo garetho General .NET 1 May 3rd, 2005 09:17 PM
is the text in text box selected? nerssi Javascript 4 September 21st, 2004 09:26 AM
Adding style to text selected by user Jonax HTML Code Clinic 3 June 9th, 2004 03:54 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.