Monday, 14 April 2014

7 jQuery Code Snippets every web developer must have

jQuery extensively simplified web developer's life and has become a leader in javascript available libraries. There are a lot of useful jQuery snippets available but here in this post I am going to share 7 basic and widely used code snippets that every front-end web developer must have. Even for those who are new to jQuery can easily understand and get benefit from these routinely used code snippets.

1. Print Page Option
Providing option to print a page is a common task for web developers. Following is the available code:

<!-- jQuery: Print Page -->



           return false;

<!-- HTML: Print Page -->

<a  class="printPage" href="#">Print</a>

2. Helping Input Field/Swap Input Field

In order to make an Input Text field helpful, we normally display some default text inside it (For Example "Company Name") and when user click on it, text disappears and user can enter the value for it.
You can try it yourself by using the following code snippet.

<!-- jQuery: Helping Input Field -->
           var $this = $(this);
           var title = $this.attr('title');
           if($this.val() == title)
}).blur(function() {
           var $this = $(this);
           var title = $this.attr('title');
           if($this.val() == '')

<!-- HTML: Swap Input Field -->

       <input type="text" 
value="Company Name" 
title="Company Name" />

3. Select/Deselect All options

Selecting or Deselecting all available checkbox options using a link on HTML page is common task.

<!-- jQuery: Select/Deselect All -->
$('.SelectAll').live('click', function(){ $(this).closest('.divAll').find('input[type=checkbox]').attr('checked', true); return false; }); $('.DeselectAll').live('click', function(){ $(this).closest('.divAll').find('input[type=checkbox]').attr('checked', false); return false; });

<!-- HTML: Select/Deselect All -->

<div class="divAll"> <a href="#" class="SelectAll">Select All</a>&nbsp; <a href="#" class="DeselectAll">Deselect All</a> <br /> <input type="checkbox" id="Lahore" /><label for="Lahore">Lahore</label> <input type="checkbox" id="Karachi" /><label for="Karachi">Karachi</label> <input type="checkbox" id="Islamabad" /><label for="Islamabad">Islamabad</label> </div>

4. Disabling Right Click

For web developers, its common to disable right click on certain pages so following code will do the job.

<!-- jQuery: Disabling Right Click -->


5. Identify which key is pressed.
Sometimes, we need to validate the input value on a textbox. For example, for "First Name" we might need to avoid numeric values. So, we need to identify which key is pressed and then perform the action accordingly.
<!-- jQuery: Which key is Pressed. -->

<!-- HTML: Which key is Pressed. -->
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>

6. Validating an email.
Validating an email address is very common task on HTML form.

<!-- jQuery: Validating an email. -->
$('#txtEmail').blur(function(e) {
            var sEmail = $('#txtEmail').val();
            if ($.trim(sEmail).length == 0) {
                alert('Please enter valid email address');
            var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]
            if (filter.test(sEmail)) {
                alert('Valid Email');
            else {
                alert('Invalid Email');

<!-- HTML: Validating an email-->
<asp:TextBox id="txtEmail" runat="server" />

7. Limiting MaxLength for TextArea
Lastly, it usual to put a textarea on a form and validate maximum number of characters on it.

<!-- jQuery: Limiting MaLength for TextArea -->
   var MaxLength = 500;
          if ($(this).val().length >= MaxLength) {

<!-- HTML: Limiting MaLength for TextArea-->
<asp:TextBox ID="txtDescription" runat="server" 
                         TextMode="MultiLine" Columns="50" Rows="5"></asp:TextBox>

This is my selection of jQuery code snippets but jQuery is a very powerful client-side framework and a lot more can be done using it.


Post a Comment


ADFS (1) ADO .Net (2) Ajax (1) Angular (1) Angular Js (17) Angular2 (28) ASP .Net (14) Authentication (1) Azure (1) Breeze.js (1) C# (50) CD (1) CI (2) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DevOps (4) DI (4) Dotnet (22) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (8) Lamda (3) Linq (11) Mongodb (1) MVC (50) NodeJS (7) RDLC (1) Report (1) SDLC (1) Sql Server (30) SSIS (3) SSO (1) SSRS (2) UI (1) WCF (13) Web Api (11) Web Service (1) XMl (1)