1

I know that the most quick way to inspect a form can be .keyup() or .keydown() but what about if the user copy/pastes some text? Tha event will be missed but the content in the input might have changed.

Also if the user presses (or lets) e.g. the alt key that event will trigger the query (uselessly in my case).

Is there any similar event handler like "text change" but in real time inspection? Like onchange but as a handler.

Diolor
  • 13,181
  • 30
  • 111
  • 179
  • 1
    You could use setInterval() method and test it every n miliseconds. – intelis Oct 24 '12 at 23:56
  • Do you want to monitor the change and format it on the way? If not then why not to pass the analysis on `blur()` instead? – Grzegorz Oct 24 '12 at 23:56
  • @Grzegorz I want to monitor the change like the typeahead effect but more efficiently (without unnecessary key press but with copy/paste-from menu- option. I can't think any other possible changes on a text input) – Diolor Oct 25 '12 at 00:02
  • Do it on all events, `change keyup focus blur` and that should work for everything even copy/paste. I did it like this in [my plugin](http://elclanrs.github.com/jq-idealforms/) and it works fine. – elclanrs Oct 25 '12 at 00:04
  • 1
    This is the absolute best way I've found: http://stackoverflow.com/questions/574941/best-way-to-track-onchange-as-you-type-in-input-type-text/574971 – Joel Mellon Oct 25 '12 at 00:06

2 Answers2

1

You could always check if the user is copy / pasting text using the bind() JQuery method. There's a similar question on Stack Overflow that can be found here: How do you handle oncut, oncopy, and onpaste in jQuery?

Community
  • 1
  • 1
1
(function($){
    $.fn.oneinput = function(callback) {        
        function testInput(){ 
            var tb = jQuery(this);
            var currentValue = tb.val();
            if (tb.data("lastInput") !== currentValue ) {
                tb.data("lastInput",currentValue );
                if(callback) { 
                    callback.call(this) 
                };
            }
            return this;
        }    
        $(this).bind("keyup input paste", testInput);
    };
}(jQuery));

$('input').oneinput( function(){ console.log(this.value); });
epascarello
  • 204,599
  • 20
  • 195
  • 236