ricardozea
2/21/2013 - 8:48 PM

jQuery: General Utility Scripts

jQuery: General Utility Scripts

$(function() {  
  //If JavaScript is available, remove class '.nojs' and add class '.js'
  $('.nojs').removeClass('nojs').addClass('js');

  //Prevents default action of links
  $('.noclick').click(function(e) {
  	e.preventDefault();
  });
  
  //Add a class to all the last <li>'s
  $('li:last-child').addClass('last-item');

  //New Window: Make all links with '_blank' and '_new' targets, use 'nw'. Plus add the title="" attribute
  $("a[target='_blank'], a[target='_new'], a[target='nw']").attr({target:'nw', title:'Opens in a new window'});
  
  //Add a class to labels with checkboxes and radio buttons and style them when checked
  $('label input[type=radio], label input[type=checkbox]').click(function() {
    $('label:has(input:checked)').addClass('active');
    $('label:has(input:not(:checked))').removeClass('active');
  });
  
  //Avoid collapsing the dropdowns when clicking inside of them
  $('.selector').click(function(e) {
    e.stopPropagation();
  });
  
  //Improve usability by highlighting the row the user needs to focus on. Adds a .focus class to .row
  $('input, select').not('input[type=checkbox]').focus(function() {
    $(this).closest('.row').addClass('focus');
  }).blur(function() {
    $(this).closest('.row').removeClass('focus');
  });
  
  //Add class .active to the corresponding item in the nav bar
  $.each($("nav a"), function(){
    var self = $(this);
    if(location.href.indexOf(self.prop('href'))>-1){
    	self.addClass('active');			
    }
  });
  
});