Tuesday, October 18, 2011

Fetching values of Controls using JQuery

Here is some links & code snippets to fetch/reset different controls value using JQuery.

(1) Remove checked for CheckBox - $("#<%= CheckBox1.ClientID %>").removeAttr("checked");
(2) Set Value for TextBox - $("#<%= TextBox1.ClientID %>").val("");
(3) Set Value for Dropdownlist - $('#<%= DropDownList1.ClientID %>').val("");
(4) Reset all Dropdownlist of Form - $("select[id*=DDL] option[value=0]").attr('selected', true);
(5) Set Value for HiddenField - $("#<%= HiddenField1.ClientID %>").val("0");
(6) Uncheck All Checkboxes on Form - $("input[type='checkbox']").attr('checked', false);
(7) Check all checkbox in grid - $('#<%=Grid1.ClientID %> >tbody >tr >td >input:checkbox').attr('checked', true);
(8) Fetch table cell value - $('#mytable tr').each(function() {
    var customerId = $(this).find("td").eq(2).html();    }
(9) Set confirm on link button in Grid -
       $('#<%= Grid1.ClientID %> tbody tr').not(':first').each(function () {
            $(this).find("a[id$=LinButton1]").click(function () {
                return confirm('Are you sure you want to delete?');
            });
        });

(10) Create common function in jQuery - this function returns checked items count in grid
$.fn.gridCount = function () {
        var vCount = 0;
        $('#<%=GridView1.ClientID %> tbody tr').not(':first').each(function () {
            var strVehicleID = $(this).find("td:first-child").find("input:checkbox");
            if (strVehicleID.attr("checked") == true) {
                vCount++;
            }
        });
        return vCount;
    }
(11) Some useful jQuery syntaxes
       jQuery Selector

(12) Fetch Url attribute values
       Get current page URL and title with jQuery 

No comments:

Post a Comment