locked
can i declare define funtions in document.ready and use them at more than few places. RRS feed

  • Question

  • User2102072086 posted

    <div>can i declare define funtions in document.ready and use them at more than few places.</div> <div> </div> <div>hi,</div> <div> </div> <div>    Q1)  should i define and declare this function inside the docuent.ready or outside.</div> <div> </div> <div>i do not need this x2() function any where else , i need it inside only. </div> <div> </div> <div>

    $(document).ready(function () {  
    
    function x2 ()
    {
    alert("abc");
    }
    
    $('#ddlcomp').change(function (event) {
     x2 ();
    });
    
    
    $('#ddlcust').change(function (event) {
     x2 ();
    });
    
    
    
    });

    </div>

    Tuesday, July 21, 2020 12:06 PM

All replies

  • User753101303 posted

    Hi,

    I expect this to work. If you have a problem it could be a typo at  (your code is missing the closing quote) :

    $('#ddlcomp')

    If your JavaScript doesn't work always use F12 Console to see if you have a JavaScript error and/or use the debugger to understand what happens - for example checking that $("ddlcust") does return something - rather than reading your code and trying to guess.

    Edit: never post code without telling what happens. My first move was to suggest trying by yourself until I noticed this possible typo.

    Tuesday, July 21, 2020 12:22 PM
  • User288213138 posted

    Hi rajemessage,

    Q1)  should i define and declare this function inside the docuent.ready or outside. i do not need this x2() function any where else , i need it inside only. 

    You can define and declare a function inside the document and call the function in other function.

    Here a demo for you as a reference.

    <asp:DropDownList ID="DropDownList1" runat="server">
                    <asp:ListItem>1</asp:ListItem>
                    <asp:ListItem>2</asp:ListItem>
                    <asp:ListItem>3</asp:ListItem>
                </asp:DropDownList>
                <asp:DropDownList ID="DropDownList2" runat="server">
                    <asp:ListItem>a</asp:ListItem>
                    <asp:ListItem>b</asp:ListItem>
                    <asp:ListItem>c</asp:ListItem>
                </asp:DropDownList>
    
    
      <script>
            $(document).ready(function () {
                function x2() {
                    alert("abc");
                }
                $('#DropDownList1').change(function (event) {
                    x2();
                });
                $('#DropDownList2').change(function (event) {
                    x2();
                });
            });
        </script>

    Best regards,

    Sam

    Wednesday, July 22, 2020 2:14 AM