0

I have two input fields as under:

<input name="formtext" id="FormText" type="text" value="first field text" />
<input name="formname" id="FormName" type="text" value="second field text" oncopy="onCopyText()" />

Also I have got Javascript code as:

function onCopyText()   {
    document.getElementById('FormText').select();
}

I want to have first field's text selected when I copy the text value of second field. It selects the value of first field just fine but in my clipboard I have the value of first field copied instead of the value of second field. What wrong am I doing?

Arvind K.
  • 1,184
  • 2
  • 13
  • 27
  • 1
    So you do not want it to copy the first? Than why are you selecting the text? – epascarello Apr 17 '15 at 12:57
  • I want the focus back to first field and have it selected so I can enter new text to be converted to slug and then copy it from second field and continue the process for new text strings. I have hundreds of strings to convert. This however is just a clarification. I am looking for a solution. – Arvind K. Apr 17 '15 at 13:00

1 Answers1

0

You need to add a delay so the selection happens after the copy.

window.setTimeout( function () {
    document.getElementById('FormText').select(); 
}, 100);
epascarello
  • 204,599
  • 20
  • 195
  • 236