TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Javascript, AJAX, E4X (http://www.talkphp.com/javascript-ajax-e4x/)
-   -   Input-field with captured text falsely empties on click (http://www.talkphp.com/javascript-ajax-e4x/3713-input-field-captured-text-falsely-empties-click.html)

sidisinsane 12-04-2008 08:11 PM

Input-field with captured text falsely empties on click
 
The script captures body-text to a specified input-field by either left-clicking and dragging the mousepointer over or simply performing a double-click on it.

The problem: When text is entered manually into the input-field, it simply disappears when a neighbouring select-element is clicked. Why?

Another minor issue is that I would actually prefer to be able to limit the capturable area to a container with an id.

Although this code doesn't make use of a framework, I wouldn't mind a solution with JQuery or MooTools.

Code:

// Captures highlighted text within the document to a specified input-field.
var text = "";

// Captures all highlighted text.
function captureText()
{
        // Sets text MSIE or Netscape active.
        lookuptext = (document.all) ? document.selection.createRange().text : document.getSelection();
       
        // Form name="research", input name="lookup"
        document.research.lookup.value = lookuptext;

        return TRUE;
}

// Initiate capture function
document.onmouseup = captureText;
if(!document.all) document.captureEvents(Event.MOUSEUP);


Wildhoney 12-04-2008 11:05 PM

1 Attachment(s)
Its behaviour for me is that when you type something and click, the text you typed in the box disappears. Is this the correct behaviour? I apologise, but I don't really understand, I don't think.

sidisinsane 12-04-2008 11:37 PM

1 Attachment(s)
Quote:

Originally Posted by Wildhoney (Post 20201)
Its behaviour for me is that when you type something and click, the text you typed in the box disappears. Is this the correct behaviour? I apologise, but I don't really understand, I don't think.

Thanks for responding!

I modified your sample-file to demonstrate the behaviour I'm getting and what I'm expecting to get. After testing it, the issues should be clearer.

Wildhoney 12-04-2008 11:57 PM

1 Attachment(s)
Thanks. I think a simple check to see if you actually selected anything should do it, shouldn't it? By adding in the following code, I seem to have fixed it. Although I'm not entirely if this is the correct behaviour:

javascript Code:
if (!lookuptext)
{
    return;
}

Wildhoney 12-05-2008 12:08 AM

As for your other problem of only selecting items from a specific DIV, I am unsure. I've never used the createRange() or getSelection() before. I am sorry, but I hope the other thing worked for you!

sidisinsane 12-05-2008 12:12 AM

Thanks a lot, Wildhoney. Your fix solves all my issues and so simple it was.


All times are GMT. The time now is 03:58 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0