0

i have these two jquery functions and one of them is not getting called.but when i comment one of them the other one gets called and vice-versa.

1st

 $(function() {
        $("[id*=tvAi1] input[type=checkbox]").bind("click",
            function() {


                var table = $(this).closest("table");

                if (table.next().length > 0 && table.next()[0].tagName == "DIV") {
                    //Is Parent CheckBox
                    var childDiv = table.next();
                    var isChecked = $(this).is(":checked");
                    if (isChecked) {
                        if ($('#CellNumberTextBox').val() == null || $('#CellNumberTextBox').val() === '') {
                            bootbox.alert(
                                "Please enter the Cell Number because you have asked for the MobileBankingService.");
                            this.checked = false;
                            $('#CellNumberTextBox').focus();
                            return false;
                        }
                    }
                    $("input[type=radio]", childDiv).each(function() {
                        if (isChecked) {

                            $(this).attr("checked", "checked");
                            return false;
                        } else {
                            $(this).removeAttr("checked");
                        }

                    });

                }
            });


        $("#SaveButton").bind("click",
            function (e) {
               
                $("#hdMobile").val("");
              
                var tv = document.getElementById("<%= tvAi1.ClientID %>");
                var chkArray = tv.getElementsByTagName("input");
               
                for (i = 0; i <= chkArray.length - 1; i++) {

                    if (i == 0) {
                        $.ajax({
                            type: "POST",
                            url: "AddNewCustomer.aspx/SetSession",
                            data: {},
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function() {

                            }
                        });
                    }

                    if (chkArray[i].type == 'radio') {

                        if (chkArray[i].checked == true) {
                            if ($('#CellNumberTextBox').val() == null || $('#CellNumberTextBox').val() === '') {
                                bootbox.alert(
                                    "Please enter the Cell Number because you have asked for the MobileBankingService.");
                                $('#CellNumberTextBox').focus();
                                $.hideprogress();
                                return false;
                            }


                            if ($("#hdMobile").val() == "" || $("#hdMobile").val() == null) {
                                $("#hdMobile").val(chkArray[i].value);
                            } else {
                                $("#hdMobile").val($("#hdMobile").val() + "," + chkArray[i].value);
                            }
                        }
                    }
                }

            });
    });

2nd one

  $(function() {
            $("[id*=tvAi] input[type=checkbox]").bind("click",
                function() {


                    var table = $(this).closest("table");

                    if (table.next().length > 0 && table.next()[0].tagName == "DIV") {
                        //Is Parent CheckBox
                        var childDiv = table.next();
                        var isChecked = $(this).is(":checked");
                        if (isChecked) {
                            if ($('#CellNumberTextBox').val() == null || $('#CellNumberTextBox').val() === '') {
                                bootbox.alert(
                                    "Please enter the Cell Number because you have asked for the SMSService.");
                                this.checked = false;
                                $('#CellNumberTextBox').focus();
                                return false;
                            }
                        }
                        $("input[type=radio]", childDiv).each(function() {
                            if (isChecked) {

                                $(this).attr("checked", "checked");
                                return false;
                            } else {
                                $(this).removeAttr("checked");
                            }

                        });

                    }
                });

            $("[id*=tvAi] input[type=radio]").bind("click",
                function() {
                    //hdSms
                    var parentDIV = $(this).closest("DIV");
                    if ($(this).is(":checked")) {

                        if ($('#CellNumberTextBox').val() == null || $('#CellNumberTextBox').val() === '') {
                            bootbox.alert("Please enter the Cell Number because you have asked for the SMSService.");
                            this.checked = false;
                            $('#CellNumberTextBox').focus();
                            return false;
                        }

                        $("input[type=checkbox]", parentDIV.prev()).attr("checked", "checked");
                    } else {
                        $("input[type=checkbox]", parentDIV.prev()).removeAttr("checked");
                    }
                });
            $("#SaveButton").bind("click",
                function(e) {
                    $("#hdSms").val("");
                    var tv = document.getElementById("<%= tvAi.ClientID %>");
                    var chkArray = tv.getElementsByTagName("input");
                    for (i = 0; i <= chkArray.length - 1; i++) {

                        if (i == 0) {
                            $.ajax({
                                type: "POST",
                                url: "AddNewCustomer.aspx/SetSession",
                                data: {},
                                contentType: "application/json; charset=utf-8",
                                dataType: "json",
                                success: function() {

                                }
                            });
                        }

                        if (chkArray[i].type == 'radio') {

                            if (chkArray[i].checked == true) {
                                if ($('#CellNumberTextBox').val() == null || $('#CellNumberTextBox').val() === '') {
                                    bootbox.alert(
                                        "Please enter the Cell Number because you have asked for the SMSService.");
                                    $('#CellNumberTextBox').focus();
                                    $.hideprogress();
                                    return false;
                                }


                                if ($("#hdSms").val() == "" || $("#hdSms").val() == null) {
                                    $("#hdSms").val(chkArray[i].value);
                                } else {
                                    $("#hdSms").val($("#hdSms").val() + "," + chkArray[i].value);
                                }
                            }
                        }
                    }

                });
        });

I tried combining them both but didnt helped.I want both of them to be called according to div enabled/disabled from my code-behind.Newbie here.Help Appreciated.

1 Answer 1

1

You may call your anonymous functions to set the jQuery's methods. Try something like this for the 2 functions:

$(function() {
  /*Do stuff... */

})();

In your code, you set two anonymous functions (or closures). This functions will be never call (at least in your example). They only can be call (in your example) if you put the braces at the end (with some optional function's parameters).

Advantages:

Closures are useful because they let you associate some data (the lexical environment) with a function that operates on that data. This has obvious parallels to object oriented programming, where objects allow us to associate some data (the object's properties) with one or more methods.

Consequently, you can use a closure anywhere that you might normally use an object with only a single method.

Please check the documentation.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response sir.i didnt get you.can you please explain a bit.
still did not worked sir.i did like you said,putting braces at the end of the function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.