Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to auto copy text value to textarea without hitting anykey?

Status
Not open for further replies.

icy111

Programmer
Dec 13, 2001
39
US
Hi all,

I am using barcode scanner. Once i scanned a barcode into a text box, i need a javascript which can auto copy to a textarea. Without users clicking any keyboard key or mouse to activate the event.

The following is my Javascript and HTML codes:

<input name=&quot;textA&quot; id=&quot;textA&quot; type=&quot;text&quot; value=&quot;<%=request(&quot;textA&quot;)%>&quot; size=&quot;20&quot; maxlength=&quot;6&quot; onKeydown=&quot;checkEnter(this);&quot;
<script language=&quot;JavaScript&quot;>
<!--
var ctr = 0;
var VArray = new Array();
function checkEnter(textEl) {
if(window.event.keyCode==13){
event.returnValue = false;
}
else {
copyText(document.form1.textA.value);
}
}

function copyText(strText) {
var currCopy = trim(document.form1.textA.value);
if (currCopy.length ==6) {
if (ctr == 0) {
VArray[ctr] = currCopy;
} else {
//check if the barcode exist in the array
for (var k=0; k<ctr; k++) {
if (VArray[k] == currCopy) {
alert(&quot;Duplicate barcode code being scanned.&quot;);
document.form1.textA.value= &quot;&quot;;
return false;
}
}
VArray[ctr] = currCopy;
}

// if no duplicate, shall copy into the TextArea
document.form1.lstbarcode.value += strText+&quot;\n&quot;;
document.form1.texta.value = &quot;&quot;;
ctr++;
} else {
alert(&quot;Please enter 6 alphanumerics for the texta.&quot;);
}
return true;
}
//-->
</script>
 
from your code, it appears that your page must be re-loading on every scan-event... otherwise <%=request(&quot;textA&quot;)%> wouldn't work... if so, why not add an &quot;onload&quot; event to do your copying?
 
Hi Mr3Putt

Thanks for your reply.
I am not sure about the onload, will all the user's entry be gone?
As after the scanning and copying steps, i have other textboxes which user has to enter.
Then finally, the user will click on the submit button to send to backend database.

Is there any other way out?

Thanks..
 
If the user has &quot;other fields&quot; to fill in, then they're going to have to touch the keyboard or mouse anyway... so you may as well make the copy-paste routine occur &quot;onChange&quot; or &quot;onBlur&quot;, like:

Code:
<input name=&quot;textA&quot; id=&quot;textA&quot;  type=&quot;text&quot; value=&quot;<%=request(&quot;textA&quot;)%>&quot; size=&quot;20&quot; maxlength=&quot;6&quot;    onChange=&quot;checkEnter(this);&quot;>

Then, when they press the TAB key to get to &quot;field#2&quot; to continue their data entry, the checkEnter function looks for a duplicate... if &quot;checkEnter()&quot; FAILS, you can add:

Code:
document.getElementById('textA').focus();

into the function and the cursor will return to the &quot;textA&quot; field.

To check the field as soon as the bar-code-scanner plops data into the field, the function will need to be triggered by whatever event is predicated by the bar-code scanner... and I don't know what that event is.

Good luck on your project.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top