Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture Copy/Paste/Select in Javascript

How can i capture the following keys in Textbox using JavaScript?

Ctl + a

Ctl + c

Ctl + v

Following is the original Situation.

I have three Textboxes for Phones numbers. Textbox1 max length is 3 , 2nd's is 3 and 3rd is 4. When user types three digits in TextBox1 the cursor moves automatically to TextBox2 same thing happens with TextBox2 as well as TextBox3. I am handling this functionality in keyup event. Now, I am parallely using your code. But it moves in keyup event as well. This case happens when all TextBoxes are filled. Now suppose I am in TextBox1 and presses Ctl + A . This moves the user to third TextBox(unacceptable case). This is the issue.

like image 880
Pankaj Avatar asked Aug 31 '26 21:08

Pankaj


1 Answers

Use the select, copy and paste events respectively. Pretty much universally supported these days.

var textBox = document.getElementById("textBoxId");
textBox.onpaste = function() {
    alert("paste");
};

Likewise for the other events. Demo here: http://jsfiddle.net/timdown/EC2Hf/

like image 100
Tim Down Avatar answered Sep 03 '26 14:09

Tim Down



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!