Tuesday 10 December 2013

7 jQuery Code Snippets every web developer must have

Every Developer Must know about these JQuery 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 -->
$('a.printPage').click(function(){

           window.print();

           return false;
}); 

<!-- HTML: Print Page -->

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


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 -->
$('input[type=text]').focus(function(){    
           var $this = $(this);
           var title = $this.attr('title');
           if($this.val() == title)
           {
               $this.val('');
           }
}).blur(function() {
           var $this = $(this);
           var title = $this.attr('title');
           if($this.val() == '')
           {
               $this.val(title);
           }
});

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

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


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 -->
$(document).bind("contextmenu",function(e){
       e.preventDefault();


   });


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. -->
$('#txtFirstName').keypress(function(event){
     alert(event.keyCode);
  });

<!-- 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');
                e.preventDefault();
            }        
            var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]
                             {2,4}|[0-9]{1,3})(\]?)$/;        
            if (filter.test(sEmail)) {
                alert('Valid Email');
            }
            else {
                alert('Invalid Email');
                e.preventDefault();
            }
        });

<!-- 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;
       $('#txtDescription').keypress(function(e)
       {
          if ($(this).val().length >= MaxLength) {
          e.preventDefault();}
       });

<!-- 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.

Avoid Duplicate Record Isertion when Page Refresh.

The Below link Provides How to Avoid Duplicate insertion records when refresh a page.

http://www.dotnetvishal.com/2012/10/avoid-duplicate-record-insertion-on.html

How to use JQuery to call a WebService (*.asmx) method?

The Below Link Provides Short introduction of using Jquery to call a method from a webservice.


http://www.tomot.de/en-us/article/8/asp.net/how-to-use-jquery-to-call-a-webservice-asmx-method

Demonstrating LINQ with SQL, DataSets, and XML Examples

Refer this link that how to use LINQ in your applications

http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b/viewsamplepack

Dynamicallly Binding jQuery UI DatePicker on dynamically created text box

Here is the complete code you should start with  this.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="robots" content="noindex,nofollow" />
<title>Dynamically attach jQuery DatePicker to Text box </title>
<link rel="stylesheet" href="/resources/themes/master.css" type="text/css" />
<link
 href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css"
 rel="stylesheet" type="text/css" />
<script
 src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"
 type="text/javascript"></script>
<script
 src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"
 type="text/javascript"></script>
<script
 src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.js"
 type="text/javascript"></script>
<script src="/resources/scripts/mysamplecode.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function () {

        var myCounter = 1;
        $(".myDate").datepicker();

        $("#moreDates").click(function () {

            $('.myTemplate')
             .clone()
             .removeClass("myTemplate")
             .addClass("additionalDate")
             .show()
             .appendTo('#importantDates');

            myCounter++;
            $('.additionalDate input[name=inputDate]').each(function (index) {
                $(this).addClass("myDate");
                $(this).attr("name", $(this).attr("name") + myCounter);
            });

            $(".myDate").on('focus', function () {
                var $this = $(this);
                if (!$this.data('datepicker')) {
                    $this.removeClass("hasDatepicker");
                    $this.datepicker();
                    $this.datepicker("show");
                }
            });

        });

    });
</script>
</head>
<body>
 <div id="allContent">

  <div id="myContent">
   <form id="samplecode" name="samplecode" method="POST" action="">
     <fieldset>
      <legend><b></b></legend>
      <div id="importantDates"> 
      <div class="myTemplate" style="display:none">
       <p>
        <input type="text" name="inputDate" size="10" value=""/>&nbsp;(mm/dd/yyyy)
       </p>
      </div>
      </div>
      <input id="moreDates" type="button" value="Add more Dates" />        
     </fieldset>
   </form>
  </div>
 </div> 

 <div></div>
</body>

</html>