user3195896
user3195896

Reputation:

how do i call a function and placing it into a method

I have a function like this:

$(document).ready(function () {

    $('.SelectID').click(function (e) { 
var output;
        var select = $(this);
        switch (select.text()){
            case "FirstOption":
                var a = ("1");
                break;
            case "SecondOption":
                var b = ("2");
                break;
        }
        var output = [a|| b];
        return output;
    }
    $(document).on("click", "#Soemthinhg", function () {
    data1 = $('#something').val();
    data2 = $(output).val();

    AjaxMethod(data1, data2 )
});
});

The question is that when I run the code it says 0x800a138f - JavaScript runtime error: Unable to get property 'toLowerCase' of undefined or null reference

Upvotes: 0

Views: 106

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388316

You can use a closure scoped variable to do this

$(document).ready(function () {
    //declare it in a closure scope so that the global scope will not get polluted
    var output;
    $('.SelectID').click(function (e) {
        var select = $(this);
        switch (select.text()) {
            case "FirstOption":
                var a = ("1");
                break;
            case "SecondOption":
                var b = ("2");
                break;
        }
        output = [a || b];
    })
    $(document).on("click", "#Soemthinhg", function () {
        data1 = $('#something').val();
        //here use the closure variable
        data2 = $(output).val();

        AjaxMethod(data1, data2)
    });
});

Using global variable(not recommended)

//declare the variable in global scope
var output;
$(document).ready(function () {
    $('.SelectID').click(function (e) {
        var select = $(this);
        switch (select.text()) {
            case "FirstOption":
                var a = ("1");
                break;
            case "SecondOption":
                var b = ("2");
                break;
        }
        output = [a || b];
    })
});
$(document).on("click", "#Soemthinhg", function () {
    data1 = $('#something').val();
    //here use the global variable
    data2 = $(output).val();

    AjaxMethod(data1, data2)
});

Upvotes: 1

suren
suren

Reputation: 981

You have declared the output variable inside document.ready function, it wont be available outside the scope, If you want to access the output variable outside of the document.ready function, declare it as a global variable.

You can declare it as a global variable like var output; outside any function

or inside the document.ready function as window.output = [a | b]

$(document).ready(function () {
        $('.SelectID').click(function (e) { 
            var select = $(this);
            switch (select.text()){
                case "FirstOption":
                    var a = ("1");
                    break;
                case "SecondOption":
                    var b = ("2");
                    break;
            }
            window.output = [a|| b];
            return output;
        }
        });

    $(document).on("click", "#Soemthinhg", function () {
        data1 = $('#something').val();
        data2 = $(output).val();

        AjaxMethod(data1, data2 )
    });

Upvotes: 0

Related Questions