jcadima
9/30/2016 - 1:24 PM

Check if element is loaded in the DOM

Check if element is loaded in the DOM





<script>
// check if menudropdown is loaded in the DOM to avoid broken JS code
var menudropdown = document.getElementById("vehicle_type") ;
if ( typeof( menudropdown ) != 'undefined' && menudropdown != null )  { 
  document.getElementById("vehicle_type").onchange = function() { 
  	switchme(this); // pass 'this' current element to the switchme function
  }      
}


function switchme(SNewSel) { 
	var chauffeur_cars =  ["Lamborghini", "Rolls", "Mercedes-Benz" , "Mercedes", "Rolls Royce" ]   ;
	var car_name = SNewSel.options[SNewSel.selectedIndex].text; // get car name from select dropdown
	//var car_name = car_index.replace("-", " ") ;
	var car_first = car_name.split(" ") ;  // get first letter of car name
	console.log(car_first[0]) ;
	console.log(car_name) ;

	// if selected car is found in the chauffeur cars show chauffeur options, else hide it
    if (jQuery.inArray(car_first[0], chauffeur_cars )!='-1') {
	    jQuery('.chauffeur_options').show() ;
       // alert(car_first[0] + ' is in the array!');
    } else {
        // alert(car_first[0] + ' is NOT in the array');
         jQuery('.chauffeur_options').hide() ;
    }	
	
	
}


</script>