$(document).ready(function() { //initate the makes makeYear(); $('#choice-area').on('change', '#year-select', function() { var year = $('#year-select').val(); //set the add button to disabled resetVariant(); makeMake(year); }); $('#choice-area').on('change', '#make-select', function() { var year = $('#year-select').val(); var make = $('#make-select').val(); //set the ad button to disabled $('#product-add #add').addClass('disabled'); //set variant to resetVariant(); makeModel(year, make); }); $('#choice-area').on('change', '#model-select', function() { //grab all selected dropdowns var year = $('#year-select').val(); var make = $('#make-select').val(); var model = $('#model-select').val(); resetVariant(); setVariant(year,make,model); }); //set alert for button click }); //run and make the first dropdown section function makeYear() { var objectLength = (sortObject.length) - 1; //start the inital loop var html = ""; var html2 = ""; var html3 = ""; for (i = 0; i <= objectLength; i++) { //mark the current name var currentYEAR = sortObject[i].YEAR; //run the loop if first item if (i === 0) { html += ""; } else { var backCheck = i - 1; var prevName = sortObject[backCheck].YEAR; if (prevName != currentYEAR) { html += ""; } } if(i == objectLength) { //remove previous choices first $('#make-select').find('option').remove(); $('#model-select').find('option').remove(); //append the newley selected year $('#year-select').append(html); $('#make-select').append(html2); $('#model-select').append(html3); } } } function makeMake(year) { var objectLength = (sortObject.length) - 1; var selectedYear = year; var html = ""; //start the name check array var nameCheck = []; for (i = 0; i <= objectLength; i++) { //mark the current name var currentMake = sortObject[i].MAKE; var currentYear = sortObject[i].YEAR; if (selectedYear == currentYear) { //set the array check to print auto var arrayCheck = false; for (t = 0; t <= nameCheck.length; t++) { if (nameCheck[t] == currentMake) { arrayCheck = true; } } //if array check true add this to list if (arrayCheck == false) { html += ""; nameCheck.push(currentMake); } } //append the options at the end if (i == objectLength) { //remove the previous choices $('#make-select').find('option').remove(); $('#model-select').find('option').remove(); $('#make-select').append(html); } } } function makeModel(year, make) { //set looper count var objectLength = (sortObject.length) - 1; //pull in variables to check against var selectedYear = year; var selectedMake = make; var html = ""; for (i = 0; i <= objectLength; i++) { //set the current object values var currentMake = sortObject[i].MAKE; var currentYear = sortObject[i].YEAR; //check if year and make are set if (selectedYear == currentYear && selectedMake == currentMake) { html += ""; } //append the options at the end if (i == objectLength) { //remove the previous choices $('#model-select').find('option').remove(); $('#model-select').append(html); } } } function setVariant(year,make,model){ //set looper length var objectLength = (sortObject.length) - 1; //set the values var selectedYear = year; var selectedMake = make; var selectedModel = model; var variantSelect = $('#product-select'); if(selectedModel != ''){ for (i = 0; i <= objectLength; i++) { var currentYear = sortObject[i].YEAR; var currentMake = sortObject[i].MAKE; var currentModel = sortObject[i].MODEL; if(selectedYear == currentYear && selectedMake == currentMake && selectedModel == currentModel){ $('#product-select option').each(function(){ var el = $(this).data('variant'); if(el == sortObject[i].SKU){ $('select #selected').removeAttr('id'); $(this).prop('selected', true); $(this).attr('id','selected') $('#add').removeClass('disabled'); } }); } } } } function resetVariant(){ $('#product-add #add').addClass('disabled'); $('#product-select option').removeAttr('selected'); }