By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,568 Members | 1,196 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,568 IT Pros & Developers. It's quick & easy.

Paste multiline text into several input fields

P: n/a
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
iv**@tda.no
Oct 2 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.

Oct 2 '05 #2

P: n/a
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.
"slyi" <ad******@gmail.com> skrev i melding
news:11**********************@z14g2000cwz.googlegr oups.com...
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.

Oct 2 '05 #3

P: n/a
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[i]

}
}
</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>

Oct 2 '05 #4

P: n/a
BTW if replace \n with \t you should be able copy to each cell on
single row

Oct 2 '05 #5

P: n/a
Figured it out. The following seem to work ok ;-)
iv**@tda.no

<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[i]) tb[i].value=cbd[i];
}
}
</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>
Oct 2 '05 #6

P: n/a
McA
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.


IE and Mozilla don't allow to paste multiline text in to regular input
field, if you do so, only first line will be inserted. Opera puts the whole
multiline string in, but replaces newline symbol(s) with two char 32.
I suggest to use textarea element istead first input.
Oct 2 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.