Entry
JavaScript: Copy: Clipboard: List: Select: How to select in list value that is copied to clipboard?
Sep 19th, 2004 19:57
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 20 September 2004 - 04:45 am ------------------
JavaScript: Copy: Clipboard: List: Select: How to select in list value
that is copied to clipboard?
---
Select a value in a list, depending on the current value on the
clipboard
(e.g. you have a list of 1000 product numbers, after you copied 1
specific
product number (e.g. A1234C) to the clipboard, automatically this value
in this (long) list is selected, so that you do not have to scroll
anymore through this list to find it, as it is now automatically
selected for you already).
---
Steps: Overview:
1. -Copy the value to the clipboard (e.g. from another program, e.g.
a database)
e.g. copy 'A1234C' to the clipboard
2. -Get this value from the clipboard
3. -Search this value in the list
1. While not found in list
next element
4. -If found in the list, then select this value in the list
1. list[ foundI ].selected = true
---
---
Steps: Worked out:
1. -Copy the value to the clipboard (e.g. from another program, e.g.
a database)
e.g. copy 'A1234C' to the clipboard
1. You do this e.g. as usual, by right clicking with your mouse,
and selecting 'copy',
or selecting the text and doing <CTRL><C>.
2. -Get this value from the clipboard
var temp1 = null;
var text1 = null;
var s = "";
//
// you must use a 'text' field
// (a 'hidden' text field will not show anything)
//
if ( typeof( document.forms.form1.text1 ) == "undefined" ) {
document.forms.form1.innerHTML += '<INPUT TYPE="text"
NAME="text1">';
}
temp1 = form1.text1.createTextRange();
temp1.execCommand( 'Paste' );
s = form1.text1.value;
// alert( s ); // clipboard is pasted to a string here
3. -Search this value in the list
1. While not found in list
next element
var I = 0
var minI = 0
var maxI = form1.list1.length - 1;
//
var foundI = -1; // some sentinel value
//
I = minI - 1;
while ( I < maxI ) {
I = I + 1;
if ( s == ( form1.list1.options[ I ].value ) ) {
foundI = I;
}
}
4. -If found in the list, then select this value in the list
if ( foundI != -1 ) {
form1.list1.options[ foundI ].selected = true;
}
5. Run the following program
1. Put the following text in a .html file
2. Run it in your browser
3. That will show a listbox
[V A ]
(together with a text box containing the text on
the clipboard)
4. Copy e.g. 'option 3' to the clipboard
then 'option 3' will be selected as soon as you load
that page
---
---
--- cut here: begin --------------------------------------------------
<HTML>
<HEAD>
<TITLE>
List: Copy: Clipboard: Select: Value
</TITLE>
<SCRIPT LANGUAGE = "JavaScript">
<!--
<!-- library: text: copy: list: select: option [kn, ri, tu, 14-09-
2004 19:10:34] -->
function PROCTextCopyListSelectValue( form1, list1 ) {
//
var temp1 = null;
var text1 = null;
var s = "";
//
// you must use a 'text' field
// (a 'hidden' text field will not show anything)
//
if ( typeof( document.forms.form1.text1 ) == "undefined" ) {
document.forms.form1.innerHTML += '<INPUT TYPE="text"
NAME="text1">';
}
temp1 = form1.text1.createTextRange();
temp1.execCommand( 'Paste' );
s = form1.text1.value;
// alert( s ); // clipboard is pasted to a string here
var I = 0
var minI = 0
var maxI = form1.list1.length - 1;
//
var foundI = -1; // some sentinel value
//
I = minI - 1;
while ( I < maxI ) {
I = I + 1;
if ( s == ( form1.list1.options[ I ].value ) ) {
foundI = I;
}
}
//
if ( foundI != -1 ) {
form1.list1.options[ foundI ].selected = true;
}
//
}
// -->
</SCRIPT>
</HEAD>
<BODY
ONLOAD= ' PROCTextCopyListSelectValue( document.forms.form1,
document.forms.form1.list1 );'>
<FORM
NAME="form1"
>
<SELECT
NAME="list1"
>
<OPTION VALUE="">Please select</OPTION>
<OPTION VALUE="option 1">A</OPTION>
<OPTION VALUE="option 2">B</OPTION>
<OPTION VALUE="option 3">C</OPTION>
<OPTION VALUE="option 4">D</OPTION>
</SELECT>
</FORM>
</BODY>
</HTML>
--- cut here: end ----------------------------------------------------
---
---
Succesfully tested on Microsoft Windows XP Professional,
using Microsoft Internet Explorer v6.
----------------------------------------------------------------------