stored context in a JQuery variable does not work

Posted by user203687 on Stack Overflow See other posts from Stack Overflow or by user203687
Published on 2010-06-18T16:40:30Z Indexed on 2010/06/18 16:43 UTC
Read the original article Hit count: 129

Filed under:
|

The following works fine:

    $(function() {
        EnableDisableButtons();
        $('#TextBox1').keyup(function() {
            EnableDisableButtons();
        });
    });

    function EnableDisableButtons() {
        var len = $('#TextBox1').val().length;
        if (len == 0) {
            $("#Button1").attr("disabled", "disabled");
        }
        else {
            $("#Button1").attr("disabled", "");
        }
    }

But the following does not work at all:

    var txt = $('#TextBox1');

    $(function() {
        EnableDisableButtons();
        txt.keyup(function() {
            EnableDisableButtons();
        });
    });

    function EnableDisableButtons() {
        var len = txt.val().length;
        if (len == 0) {
            $("#Button1").attr("disabled", "disabled");
        }
        else {
            $("#Button1").attr("disabled", "");
        }
    }

The error it was throwing was " 'txt.val().length' is null or not an object". Can anyone help me on this.

thanks

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about jquery-selectors