locked
how to make a generic function? RRS feed

  • Question

  • User-2101450227 posted

    hi,

    please can any one tell me how to make a generic function in jquery.

    please help me out.

    thanks for the help.

     

    Monday, November 30, 2015 7:12 AM

Answers

  • User1724605321 posted

    Hi immad,

    Please explain more about your requirement . The basic consideration is to create a function and pass parameters into it ,for example:

      function ChangeStatus(url, refTextbox) {
                var msgbox = $("#status");
                $.ajax({
                    type: "POST",
                    url: url,
                    data: "{'args': '" + refTextbox.val() + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        if (msg.d != null) {
                            refTextbox.val(msg.d);
                            refTextbox.removeClass("notavailablecss");
                            refTextbox.addClass("availablecss");
                            msgbox.html('<img src="../Images/a.png"> <font color="Green">  </font>');
                        }
                        else {
                            refTextbox.removeClass("availablecss");
                            refTextbox.addClass("notavailablecss");
                            msgbox.html(msg.d);
                            refTextbox.val('');
                            refTextbox.focus();
                        }
                    }
                });
            }

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 30, 2015 8:23 AM
  • User475983607 posted

    If the goal is to create a single JQuery AJAX method that can be called from any page, then you'll need to move all the local variables to method parameters.  Things like data, URL, action (GET/POST) etc.  This shifts the responsibility of populating these value to the calling function.  

    The design is dependent on the application requirements. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 30, 2015 1:35 PM

All replies

  • User-2101450227 posted

    hi,

    this is my function

    <script type="text/javascript">
        $(document).ready(function () {
            $("#<%=txtVendorID.ClientID%>").change(function () {
                var msgbox = $("#status");
                $.ajax({
                    type: "POST",
                    url: "PayableTransaction_List.aspx/CheckVendorId",
                    data: "{'args': '" + $("#<%=txtVendorID.ClientID%>").val()+ "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        if (msg.d != null) {
                            $("#<%=txtVendorID.ClientID%>").val(msg.d);
                            $("#<%=txtVendorID.ClientID%>").removeClass("notavailablecss");
                            $("#<%=txtVendorID.ClientID%>").addClass("availablecss");
                            msgbox.html('<img src="../Images/a.png"> <font color="Green">  </font>');
                        } 
                        else {
                            $("#<%=txtVendorID.ClientID%>").removeClass("availablecss");
                            $("#<%=txtVendorID.ClientID%>").addClass("notavailablecss");
                            msgbox.html(msg.d);
                            $("#<%=txtVendorID.ClientID%>").val('');
                            $("#<%=txtVendorID.ClientID%>").focus();
                        }
                    }
                });
            });
        });
    </script>


    how i  make this function in generic.

    please help me out

    thanks for the help

    Monday, November 30, 2015 7:40 AM
  • User571301025 posted

    Can you add some more clarity on the term "generic function"? What you want to achieve exactly?

    Monday, November 30, 2015 7:45 AM
  • User1724605321 posted

    Hi immad,

    Please explain more about your requirement . The basic consideration is to create a function and pass parameters into it ,for example:

      function ChangeStatus(url, refTextbox) {
                var msgbox = $("#status");
                $.ajax({
                    type: "POST",
                    url: url,
                    data: "{'args': '" + refTextbox.val() + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        if (msg.d != null) {
                            refTextbox.val(msg.d);
                            refTextbox.removeClass("notavailablecss");
                            refTextbox.addClass("availablecss");
                            msgbox.html('<img src="../Images/a.png"> <font color="Green">  </font>');
                        }
                        else {
                            refTextbox.removeClass("availablecss");
                            refTextbox.addClass("notavailablecss");
                            msgbox.html(msg.d);
                            refTextbox.val('');
                            refTextbox.focus();
                        }
                    }
                });
            }

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 30, 2015 8:23 AM
  • User-2101450227 posted

    hi,

    can u tell me where to write this code.

    thanks for the help.

    Monday, November 30, 2015 10:32 AM
  • User475983607 posted

    If the goal is to create a single JQuery AJAX method that can be called from any page, then you'll need to move all the local variables to method parameters.  Things like data, URL, action (GET/POST) etc.  This shifts the responsibility of populating these value to the calling function.  

    The design is dependent on the application requirements. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 30, 2015 1:35 PM