Paste multiline text into several input fields

  • Thread starter Iver Erling Årva
  • Start date
I

Iver Erling Årva

I am looking for a way to let the users copy e.g. a multi-line address from
a textfile and paste it into a webpage where there is one input field for
each address line in such a way that not only the first line is pasted, but
instead the program automatically jumps to the next field and put line 2 in
there and so on.

Can this be done? I tried with onkeydown to check for event.keyCode == 13,
but it doesn't seem to work with paste.

Brgds
(e-mail address removed)
 
S

slyi

you could put an onchange event on your top input box and use the split
function on each '\n' (if exists inside the pasted text) and loop
through your splited text array and place the element text into each of
your input boxs.
 
I

Iver Erling Årva

That doesn't seem to work unfortunately, as the pasted text seem to have
been stripped for anything after the first \n at that point... I need a way
to pick up the entire text from the clipboard prior to the actual paste.e.g.
when the ctrl-v is pressed.
 
S

slyi

try
<html >
<head>
<title>Untitled Page</title>
<script language="javascript">
function pasteall(){
allrows = window.clipboardData.getData('Text').split('\n');
for (var i=0;i<allrows.length;i++){
textrow='Text'+(i+1);
document.getElementById(textrow).innerText=allrows

}
}
</script>
</head>
<body>
<input type=text id="Text1" onpaste="pasteall()" />
<br />
<input id="Text2" type="text" />
<br />
<input id="Text3" type="text" />
<br />
<input id="Text4" type="text" />

</body>
</html>
 
I

Iver Erling Årva

Figured it out. The following seem to work ok ;-)
(e-mail address removed)

<html>
<head>
<title>Test</title>
<script type=text/javascript>
function chkKey(){
tb = document.all["tb"]
var cbdata=window.clipboardData.getData("text");
cbd=cbdata.split("\n");
for (i=1;i<cbd.length;i++){
if (tb) tb.value=cbd;
}
}
</script>
</head>

<body onload="form.T1.focus();">
<form name='form'>
<p>
<input id="tb" type="text" name="T1" size="20" onbeforepaste='chkKey();'>
<input id="tb" type="text" name="T2" size="20">
<input id="tb" type="text" name="T3" size="20">
</p>
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top