-1

I'm having problems with a script. I'm using a jQuery.post function inside a setTimout and it returns TypeError: g.nodeName is undefined on Firebug. Here's my script:

jQuery(function($) {
var timer;

    $("#tabela-orcamento .item .item-qtd .qtd-item").keyup(function() {     
        clearTimeout(timer);

        timer = setTimeout(function() {         
            $.post("../../aj_orc.php?op=atualizaQtd", {
                item: $(this).parents(".item").attr("data-item"),
                qtd: $(this).val()
            }, function(data) {
                $("#retornos").html(data);
            });
        },1000);
    });
});

Is something wrong?

Ricardo
  • 11
  • 1
  • 1

1 Answers1

4

Your running into a problem, because the this inside your timeout refers to another context as you might think. Just introduce another that as a intermediate variable:

jQuery(function($) {
    var timer;

    $("#tabela-orcamento .item .item-qtd .qtd-item").keyup(function() {     
        clearTimeout(timer);

        var $that = $(this);
        timer = setTimeout(function() {         
            $.post("../../aj_orc.php?op=atualizaQtd", {
                item: $that.parents(".item").attr("data-item"),
                qtd: $that.val()
            }, function(data) {
                $("#retornos").html(data);
            });
        },1000);
    });
});
Sirko
  • 72,589
  • 19
  • 149
  • 183