I am writing a script that loops through an object to first get the elements and then I am trying to loop through the value of the elements so i can change the CSS. Here is the script:
$(document).ready(function() {
var carParts = {
doors: {
flDoor: 'LOCKED',
blDoor: 'LOCKED',
frDoor: 'LOCKED',
brDoor: 'LOCKED'
}
}
var lights = 'OFF';
if (lights === 'OFF') {
$('.lights').css("display", "none");
}
for(var i in carParts.doors) {
for(var key in carParts.doors) {
var value = carParts.doors[key];
console.log(value);
if (value === 'locked') {
$('.'+i+'-open').css("display", "none");
}
}
}
})
So i want to loop through the carparts.door object and get the actual flDoor, blDoor, frDoor, brDoor. These names are needed in the final jquery line to assign the proper class name. I don't want that line to fire unless the actual value is locked though. I'm newer to javascript and still learning. I appreciate any feedback you have on the code.
var lights = 'OFF'; if (lights === 'OFF')should just beif(true);)LOCKEDagainst the stringlockedwithin thefor...inloop: JavaScript is case-sensitive, you need to pay attention to that (or use.toLowerCase()in string-comparisons).