0

I am trying to find a way to get more than one characters pasted on html text field (input type=text), using onkeydown or onkeypress, I am NOT interested in onkeyup(it works), so please do not suggest me this solution. Also I am not interested in Jquery, I do not like to use it. I need solution to work with all browsers.

I am able to do that if you type one character, by taking the character of the event, but till now I am unable to get group of characters come from the paste (ctrl+V) or by mouse.

You can look at Facebook search menu that shows auto complete results, it works on events: onkeydown and onkeypress and you notice that you get the result before you release your finger, try to paste something (more than one character) and you get the result before releasing finger, how? Onkeypress and onkeydown do not show first thing you paste or type because they happen before the text being added to text field.

If you want to understand what I want more, please check my code below:

 <input type="text" id="targetTextField" onkeydown="CapturePastedString(this.id)" />



<script>
function CapturePastedString(id){

var targetTextField=document.getElementById(id);

// below I need to capture the pasted string like:
var pasted_string= function(){.....}

targetTextField.value=pasted_string;

}
</script>

Any help would be so thankful, because since long time I am trying but cannot find solution yet.

moderns
  • 650
  • 10
  • 23
  • check out the [onpaste event](http://help.dottoro.com/ljuimtmq.php) – malificent Oct 11 '11 at 20:02
  • But it is not compatible with all browsers :( It cannot be a solution.. I already checked that. Can anybody tell me what the secret for this problem?? I really cannot find solution after hundred hours search and calls for developers?!!! – moderns Oct 11 '11 at 20:53
  • Any suggestions guy? Is it that much hard that I cannot get answer for this question since 1 year?!! – moderns Oct 12 '11 at 20:07
  • The problem with the onkeydown and onkeypress events is that the actual input hasn't been written to the textbox yet. So you're not going to be able to know what the user input during those events. Check out this thread: http://stackoverflow.com/questions/146916/javascript-events-getting-notified-of-changes-in-an-input-control-value. Not the exact same as what you're looking for but perhaps you could create a custom listener. It looks like you'll still have to browser sniff as not all browsers implement this in the same way. – malificent Oct 12 '11 at 21:39
  • Thanks a lot, I hope creating custom event listeners is supported in all browsers, I did some research, it's not common :S, please if you have easy examples to read will be so thankful, and will try to get that soon. Appreciate your hints!! – moderns Oct 13 '11 at 03:10

1 Answers1

0

Try the onkeydown handler it works.You can get the keyCode from there and then the key.

<input type="text" value="" onkeydown="alert(String.fromCharCode(event.keyCode));"/>

Note that since all characters on the keyboard are capital you would also get capital letter. The above code would display the key pressed

prashant
  • 1,382
  • 1
  • 13
  • 19