Skip to main content

Posts

Showing posts from December, 2011

jQuery : Select all checkboxes in Grid View

While working with Grid View control one of the most common scenarios encountered is that select all child checkboxes. Here I would like to share a quick and really easy code snippet that I have written in jQuery. Let’s start with Grid View code below, here key points are 1) Id of <HeaderTemplate> checkbox.            id="chkParent" 2) CssClass of <ItemTemplate> checkbox.       CssClass="chkChild" <asp:GridView ID="gvMemberBuddies" runat="server" AutoGenerateColumns="false" CellPadding="2" CellSpacing="0" Width="100%"> <Columns> <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="50px" > <HeaderTemplate> <input id="chkParent" name="chkParent" type="checkbox"> </HeaderTemplate> <ItemTemplate> <asp:Ch...

Retrieving query string parameter's value using JavaScript

Today, while working on my project I needed to get query string parameter’s value. Here I would like to share a javascript code snippet that returns an object with querystring parameter and respective value. JavaScript function that returns key/value pairs object: function GetQueryStringParameters(){ var qstring = window.location.search.replace('?', '').split('&'); var qObj = {}; for (var i = 0; i < qstring.length; i++) { var parametername = qstring[i].split('=')[0]; var parametervalue = qstring[i].split('=')[1]; qObj[parametername] = parametervalue; } return qObj; } Example: Let’s assume we have following URL: http://localhost/testpage.aspx?param1=value1&param2=value2 Code to get Object of querystring parameter’s value: $(document).ready(function(){ var qparams = GetQueryStringParameters(); // fetch al...

jQuery: Finding Sibling Position

There have been many times we need to find out the position of an element in respect to its siblings. Here’s a function I‘ve created that returns the position (index) of an element in respect to its siblings. function GetElementPosition(element) { return $(element).parent().children().index($(element)); } Let’s assume we have seven <li> tags, we want to know the position of the element once we click on it. <ul id="weeklist"> <li>Sunday</li> <li>Monday</li> <li>Tuesday</li> <li>Wednesday</li> <li>Thursday</li> <li>Friday</li> <li>Saturday</li> </ul> jQuery Code: $(document).ready(function() { $("#weeklist li").bind('click', function(){ alert( "position: " + GetElementPosition(this)); }); }); Try the demo below, Click on list element to get the position of the element Sunday ...