jareddreyer
2/2/2016 - 9:52 PM

jquery and javascript snippets

jquery and javascript snippets

//check every input of type checkbox and remove the checked state

$('input[type=checkbox]').each(function() 
{ 
    $(this).prop('checked', false); 
});
//get object of parents up 2nth
$('#element1').parents()[2];

//compare object to selector
if($(this).closest('.slide')[0] == $('.slide').last()[0])
{ 
  //do stuff 
  return;
}
$(document).ready( function () {

	$("a[href$='.pdf']").on('click', function()
	{
	  //do stuff
	});

});
//selects a text element, then splits by whitespace and stylises the first part
$('#page .subnav h1').text(function(i,txt)
{
    var firstWord = $('#page .subnav h1').text().split(' ')[ 0 ]; 
  
    //wrap it with span
    var replaceWord = '<span class="h1-firstword">' + firstWord + '</span>';

    //create new string with span included
    var newString = $(this).html().replace(firstWord, replaceWord);

    //apply to the divs
    $(this).html(newString);
  
});
$(function() 
{
	// find all links for update_your_details_on_VNR and change file type from .html to .php
	$('a[href$="update_your_details_on_VNR.html"]').each(function(index,element)
	{
    	element.href = element.href.replace('.html','.php');
    });
    
});
var myData = [];
$('.contacts-block-input-text').each(function(){
  myData.push({
      id: $(this).attr('id'),
      value: $(this).val()
  });
});

$.ajax({
        url: site+form, 
        type: 'POST',
        data: myData,
        success: function(response)
        {
          //dostuff
      });
 $(function()
 {
     $('#go').attr("disabled", 'disabled');
	 
     $('.regions').change(function()
	 {
         var regions =
             ".NorthernRegion, .CentralRegion, .LowerRegion, .SouthernRegion";
         if ($(this).val() != 'null') {
             var selecteditem = $('.regions option:selected').val();
             $(regions).hide();
             $('.' + selecteditem).show();
             $('#go').removeAttr("disabled");
         } else {
             $(regions).hide();
             $('#go').attr("disabled", 'disabled');
         }
         return false;
     });
	 
     $('#go').live("click", function()
	 {
         var selecteditem = $('.regions option:selected').val();
         var site = $('.' + selecteditem).val();
         var sitetext = $('.' + selecteditem + " option:selected").text();
		
		
         if (selecteditem != 'null')
		 {
            var confirmationText = site.split('?')[1];
			
			switch(confirmationText)
			{
				case "text1":
					alert($('.ConfirmationText option[value="' + confirmationText + '"]').text());
					window.location.href = site;
					break;
				case "text2":
					alert("Taking you to " + sitetext + " forms'");
					window.location.href = site;
					break;
				default: 
					alert($('.ConfirmationText option[value="' + confirmationText + '"]').text());
					window.location.href = site;
			}
            return false;
         }
     });
 });
//take json object and rewrite keys in new json object
var jsonObj = $.parseJSON(response);
var arr = [];

for(var i = 0, len = jsonObj .length; i < len; i++)
{
  arr.push( {"id" : jsonObj[i].skill_id, "name": jsonObj[i].skill_name });
}

console.log(arr);
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
$(function() {
$('.multi-field-wrapper').each(function() {
    var $wrapper = $('.multi-fields', this);
    $(".add-field").click(function(e) {
    console.log($wrapper);
        $('.multi-field:first-child', $wrapper ).clone(true).appendTo($wrapper).find('input').val('').focus();
        $('button.remove-field').show();
        
        
    });
    $('.multi-field .remove-field', $wrapper).click(function() {
        if ($('.multi-field', $wrapper).length > 1)
            $(this).parent('.multi-field').remove();
       if ($('.multi-field', $wrapper).length == 1)
       	$('button.remove-field').hide();
    });
});

//take all fields and put them into hidden input field, separated by ',' delimiter.
 $("#form_email_834345_submit").click(function(e) {
        var txtData = [];
        $(".multi-field-wrapper :input[type=text]").each(function() {
            txtData.push($(this).val());
        });
        $("#q834428_q12").val(txtData.join(", "));
    });

});
$(function() {
$('.multi-field-wrapper').each(function() {
    var $wrapper = $('.multi-fields', this);
    $(".add-field").click(function(e)
    {
        $('.multi-field:first-child', $wrapper ).clone(true).appendTo($wrapper).find('input').val('').focus();
        $('button.remove-field').show();
    });
    $('.remove-field').click(function() {       
       
        $('.multi-field:last').remove();
       if ($('.multi-field', $wrapper).length == 1)
       	$('button.remove-field').hide();
    });
});

 $("#form_email_808052_submit").click(function(e) {
        var vals = $('.multi-fields input').map(function()
        {
         return this.className.substring(0,9) == 'surname'?this.value+',':this.value;
        }).get().join(' ');
        vals = vals.slice(0, -1);
        $('#q808111_q10').val(vals);
    });

});
function deleteRow(cellButton) {
    var row = $(cellButton).closest('tr')
        .children('td')
    	.css({ backgroundColor: "red", color: "white" });
    setTimeout(function () {
            $(row)
            .animate({ paddingTop: 0, paddingBottom: 0 }, 500)
            .wrapInner('<div />')
            .children()
            .slideUp(500, function() { $(this).closest('tr').remove(); });
    	}, 350
    );
};
function csvtolist(selector) {
    var cites = selector;
    for (var i = 0; i < cites.length; i++) {
        var output = "";
        var text = cites[i].innerHTML;
        var list = text.split(",");
        var cleanList = [];
        for (var j = 0; j < list.length; j++) {
            if (list[j].trim().length === 0) continue;
            cleanList.push(list[j]);
        }
        var half = cleanList.length / 2;
        var list1 = cleanList.slice(0, half);
        var list2 = cleanList.slice(half);
        output += '<ul class="left">';
        for (var j = 0; j < list1.length; j++) output += "<li>" + list1[j] +
            "</li>";
        output += "</ul>";
        output += '<ul class="right">';
        for (var j = 0; j < list2.length; j++) output += "<li>" + list2[j] +
            "</li>";
        output += "</ul>";
        cites[i].innerHTML = output;
    }
}

$(document).ready(function() {
    csvtolist($(".cites p"));
    csvtolist($(".cited p"));
});
//log line number 
console.log((new Error).lineNumber);