Useful JavaScript and Jquery Code Snippets

JavaScript and Jquery  is used on thousands upon thousands of web pages. It’s one of the most common libraries to insert into pages, and it makes DOM manipulation a snap. Using Jquery  you will be able to change an existing element within a number of ways as well as: decreasing page load time and thereby giving your end users a better experience when visiting your site.

I have put together a series of helpful Jquery code snippets you may save and copy to use at your own discretion.

1. Allow Numeric Input
Usage:

 <input type="text" id="txtCount" runat="server" onkeypress="ValidateNumber(this);"  /> 
JavaScript :
 
function ValidateNumber(evt) {  
  evt = (evt) ? evt : window.event  
  var charCode = (evt.which) ? evt.which : evt.keyCode  
  if (charCode > 31 && (charCode < 48 || charCode > 57)) {  
     return false  
  }  
   return true  
}  
2. Currency Format
Usage:  
   <input type="text" id="txtMinimumPurchase"  value="0.00" onblur="this.value=formatCurrencyNumber(this.value);" />  
    JavaScript :
function formatCurrencyNumber(num) {  
  num = num.toString().replace(/\$|\,/g, '');  
  if (isNaN(num))  
  num = "0";  
  sign = (num == (num = Math.abs(num)));  
  num = Math.floor(num * 100 + 0.50000000001);  
  cents = num % 100;  
  num = Math.floor(num / 100).toString();  
  if (cents < 10)  
  cents = "0" + cents;  
  for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)  
    num = num.substring(0, num.length - (4 * i + 3)) + ',' +  
    num.substring(num.length - (4 * i + 3));  
    return (((sign) ? '' : '-') + num + '.' + cents);  
 }    
3. Number of Days of a Month and Checking Leap Year
 
 function IsleapYear(year) {  
   var isLeapYear = year % 4; // == 0  
   if (isLeapYear == 0)  
       return true;  
   else  
       return false;  
   }  

   function GetNumberofDaysinMonth(month, year) {  
     var noOfDays;  
     switch (parseInt(month)) {  
     case 1:  
     noOfDays = 31;  
     break;  
     case 2:  
     noOfDays = parseInt(IsleapYear(year) == true ? 29 : 28);  
     break;  
     case 3:  
     noOfDays = 31;  
     break;  
     case 4:  
     noOfDays = 30;  
     break;  
     case 5:  
     noOfDays = 31;  
     break;  
     case 6:  
     noOfDays = 30;  
     break;  
     case 7:  
     noOfDays = 31;  
     break;  
     case 8:  
     noOfDays = 31;  
     break;  
     case 9:  
     noOfDays = 30;  
     break;  
     case 10:  
     noOfDays = 31;  
     break;  
     case 11:  
     noOfDays = 30;  
     break;  
     case 12:  
     noOfDays = 31;  
     break;  
     }  
     return noOfDays;  
}   
4. Scroll Page to a Specific Element
 
1:  function ScrollToElement(el) {  
2:    try {  
3:      $('html, body').animate({ scrollTop: $(el).offset().top - 200 }, 'slow');  
4:    }  
5:    catch (err) {  
6:    }  
7:  }  
Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment