views:

34

answers:

2

I am using jquery on asp.net mvc. I have textbox on page and I hooked up handler for keyup event of textbox. When user deletes the text or pastes text into it, i do not get the handler called.

Please help me how to handle this.

EDIT: Also I want to get the value when user pastes the value using mouse.

A: 

Try moving your keyup to a change and see if that helps.

- EDIT -

Have a look at http://archive.devx.com/dhtml/articles/nz012402/nz012402-6.asp as well. most of this is covered in great detail.

Lance May
change event gives me the value after changing the value and textbox losing the focus. I want the value as soon as user enters at each keystroke.
mohang
@Mohang: Understood. Did you look over the article on masking input? It should have the information you're looking for.
Lance May
Actually I opened it and I was not having the time read through all that text now as I have this thing to be delivered urgently. sorry.
mohang
+2  A: 
<input type="text" id="mytextbox" />

<script type="text/javascript">
  $(document).ready(function () {
     $("#mytextbox")keyup(function(evt) {
       evt = evt || event;
       switch (evt.keyCode) {
          case 8: //Backspace was pressed
            alert("Backspace");
            break;
          case 46: //Delete was pressed
            alert("Delete");
            break;
          case 67:
            if (evt.ctrlKey) {
               alert("Ctrl-C");
            }
            break;
          case 86:
            if (evt.ctrlKey) {
              alert("Ctrl-V");

            }
            break;
          default:
            alert(evt.keyCode);
            break;
       }
     });
  });

</script>
John Hartsock
any readson for the downvote?
John Hartsock
The question was how to get it to work when the user pastes text into the box. This won't work in that instance. (Though the down vote wasn't mine.)
JacobM
@John: Apologies, but the OP stated in the question that he already had a `keyup` event but that it wasn't getting called on those circumstances. I just don't see how his post is at all addressed by this answer.
Lance May
lol. The question specifically asks how to handle text pasting using keyup event. Now clearly you can handle pasting text via mouse events as well. But it didnt specify using a mouse event. Onchange doesnt necessarly specify that text was pasted. I simply answered the question as I interpreted it.
John Hartsock
Your question has made me realize that i have to handle paste using mouse. :)
mohang
@John: I promise I meant no offense. But actually, the question specifically states that when a user deletes or pastes text that a `keyup` (that he already has) isn't getting called. I interpreted that as wanting to know when anything changes in the box.
Lance May
Hey Lance I understand completely...We just both interpreted the question differently, and thats whats cool about this site. Many opinions. It just I appreciate a comment when downvoted, because I like to know what I did wrong (it helps me and the person asking the question learn)
John Hartsock
Your solution is working. I have upvoted you for the effort. Now please provide solution for the mouse right click and paste scenario. thanks.
mohang
mohang...yeah Im still thinking about that one...there is a paste and copy event but it is not cross browser compatable. see here http://www.quirksmode.org/dom/events/cutcopypaste.html
John Hartsock
Never mind. I just took care of it on blur event. Thanks for the effort.
mohang