1

I am using this example with jQuery 1.9.1

How to delay the .keyup() handler until the user stops typing?

to delay the keyup request after user stop typing.

    // Custom Delay Function
    var delay = (function(){
        var timer = 0;

        return function(callback, ms){
            clearTimeout (timer);
            timer = setTimeout(callback, ms);
          };
    })();

    // Match Old Password
    $('input[name="old_password"]').keyup(function(){
        delay(function(){
            var data = $.trim($(this).val());
            // Send request to check
            /*$.post('admin/ajax/passReq.php', {action: 'old_match', data: data}, function(response){
                console.log('working');
            });*/
            console.log('working');
          }, 2000 );
    });

but i am getting the typeError: o.nodeName is undefined in jquery :(

is this not working on 1.9.1 or i have to use this with another way?

UPDATE: http://jsfiddle.net/jogesh_pi/6mnRj/1/

Community
  • 1
  • 1
jogesh_pi
  • 9,762
  • 4
  • 37
  • 65

3 Answers3

1

You use this inside of the delay call. And $(this) will not be the textbox.

Move it outside of the delay function call:

$('input[name="old_password"]').keyup(function(){
    var el = $(this);
    // ^^^^^^^^^^^^^
    delay(function(){
      ...
      }, 2000 );
});
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
0
// Match Old Password
$('input[name="old_password"]').keyup(function(){
    var el = $(this); //you need this this.
    delay(function(){
        var data = $.trim(el.val());
        // Send request to check
        /*$.post('admin/ajax/passReq.php', {action: 'old_match', data: data}, function(response){
            console.log('working');
        });*/
        console.log('working');
      }, 2000 );
});
Ralph Caraveo
  • 10,025
  • 7
  • 40
  • 52
  • 3
    Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime – mkoryak Jul 26 '13 at 05:11
  • @mkoryak hey, I properly called out the context of the 'this' keyword. It was most likely a simple oversight. – Ralph Caraveo Jul 26 '13 at 05:15
  • 1
    i guess what i mean is, "you need this this" does not seem to explain much in this answer. he is more likely to paste your working code in and come back in 2 weeks asking the same question – mkoryak Jul 26 '13 at 05:16
0

Change

var data = $.trim($(this).val());

to

var data = $.trim($('input[name="old_password"]').val());

You code is almost correct actually.

zs2020
  • 53,766
  • 29
  • 154
  • 219
  • what if that selector returns more than one of those? - this advice does not solve this problem generically – mkoryak Jul 26 '13 at 05:13
  • @mkoryak That's a design issue. He should use different name for the control. I basically assume the design is correct and some basic stuff should be coded in normal way. – zs2020 Jul 26 '13 at 05:15
  • there is a way to answer this question so that the selectors do not matter – mkoryak Jul 26 '13 at 05:17