Hello Fellows :)

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.

Recommended Answers

All 9 Replies

Till now I am unable to do it :( Any hints will be so thankful!

You may read this post http://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/385613 and just look at how he controls the keyboard event. You need to modify the part where the key code is being checked. That should do it.

Actually it's not what I want :( I am looking to paste string (with more than one character), and the post you provided explains about typing one character which I already know. I hope you got me, I mentioned that in my original post.

Thanks.

OK, not really. What you may want is to read the text value from the field & add the character you capture from the event. Now you can parse the string you want? For example, the field already has value "myO" and then you type in "L" using keydown. The key event capture the "L" for you. Then, in your key event, you get the value from the field which could be "myO" and then concatenate with the character you just capture to get "myOL" and do something with it?

OK, not really. What you may want is to read the text value from the field & add the character you capture from the event. Now you can parse the string you want? For example, the field already has value "myO" and then you type in "L" using keydown. The key event capture the "L" for you. Then, in your key event, you get the value from the field which could be "myO" and then concatenate with the character you just capture to get "myOL" and do something with it?

But I want to use pasting of string "hello" using Ctrl+V, so how can I add it to old value of text box? Facebook is doing that, but how??

If you want to see how FB does it, view the source (especially using Firebug of FF to see many other things). Also, please keep in mind that FB has its own implementation in many ways that are not the same as in public. You could try to accomplish as they are doing, but don't expect to get the exactly same result.

If you want to see how FB does it, view the source (especially using Firebug of FF to see many other things). Also, please keep in mind that FB has its own implementation in many ways that are not the same as in public. You could try to accomplish as they are doing, but don't expect to get the exactly same result.

If I can find the solution in the FB codes, then I will not come and ask here :) I couldn't understand because million lines of javascript... I hope someone will give me serious hint here.. I am waiting...

onbeforepaste event anybody?

it is not compatible with all browsers and doesnt have standards, I am really surprise why cannot find solution yet, FB is not doing magic but I failed to implement that :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.