Paste multiline text into several input fields

Discussion in 'Javascript' started by Iver Erling Årva, Oct 2, 2005.

  1. 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
    Iver Erling Årva, Oct 2, 2005
    #1
    1. Advertising

  2. Iver Erling Årva

    slyi Guest

    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.
    slyi, Oct 2, 2005
    #2
    1. Advertising

  3. 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" <> skrev i melding
    news:...
    > 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.
    >
    Iver Erling Årva, Oct 2, 2005
    #3
  4. Iver Erling Årva

    slyi Guest

    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>
    slyi, Oct 2, 2005
    #4
  5. Iver Erling Årva

    slyi Guest

    BTW if replace \n with \t you should be able copy to each cell on
    single row
    slyi, Oct 2, 2005
    #5
  6. Figured it out. The following seem to work ok ;-)


    <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>
    Iver Erling Årva, Oct 2, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cw
    Replies:
    1
    Views:
    620
    James J. Foster
    Jun 27, 2003
  2. PhuWong
    Replies:
    0
    Views:
    424
    PhuWong
    Feb 25, 2006
  3. Sindhu Up
    Replies:
    0
    Views:
    99
    Sindhu Up
    Apr 16, 2009
  4. dale zhang
    Replies:
    8
    Views:
    409
    Tintin
    Nov 30, 2004
  5. Replies:
    23
    Views:
    479
    Tim Streater
    Dec 18, 2013
Loading...

Share This Page