So recently I tried making a mode function that changes color when I click on different mode there are (“Cool”, “Heat”, “Fan”, “Dry”, “Auto”) and lets say I assigned a color for each of them example: Cool = Blue , Heat = Orange etc.
Based on the exact css, functions and event listener I provide why im facing this problem:
- The .heat-icon-parent for the word “Auto” does not inherit the same color of .heat-icon-parent for the word “heat”? like if I last click on heat the color is orange i want my auto also to be orange and when i click cool and color is blue i want my auto when click is blue too.
this is my code:
// Variable to store the last clicked mode
var lastClickedMode = 'cool-mode';
// Mode Toggle
$('.heat-icon-parent').click(modeToggle);
$('.heat-icon-parent#cool-mode').addClass('clicked');
$('.heat-icon-parent').click(modeColor);
// Set the fill color based on the 'clicked' class for all SVG paths
$('.heat-icon-parent svg path').css('fill', function () {
return $(this).closest('.heat-icon-parent').hasClass('clicked') ? '#ffff' : '#AEAEAE';
});
function modeToggle() {
// Check if this element is already clicked
var isClicked = $(this).hasClass('clicked');
// Remove 'clicked' class from all elements
$('.heat-icon-parent#cool-mode, .heat-icon-parent#heat-mode, .heat-icon-parent#fan-mode, .heat-icon-parent#dry-mode, .heat-icon-parent#auto-mode').removeClass('clicked');
// Set the fill color based on the 'clicked' class for all SVG paths
$('.heat-icon-parent svg path').css('fill', '#AEAEAE');
// Toggle the 'clicked' class only if it wasn't already clicked
if (!isClicked) {
$(this).addClass('clicked');
// Update the last clicked mode
lastClickedMode = $(this).attr('id');
// Set the fill color for the clicked element
$(this).find('svg path').css('fill', '#ffff');
}
}
function modeColor() {
$('.heat-icon-parent').css('background', '');
// Get the mode associated with the clicked element
var mode = $(this).attr('id');
// Update the background color based on the mode
switch (mode) {
case 'cool-mode':
$('.heat-icon-parent.clicked').css('background', 'linear-gradient(141.67deg, #56aff3, #1c71c8)');
break;
case 'heat-mode':
$('.heat-icon-parent.clicked').css('background', 'linear-gradient(141.67deg, #FF8D5D, #e67e22)');
break;
case 'fan-mode':
$('.heat-icon-parent.clicked').css('background', 'linear-gradient(141.67deg, lime, green');
break;
case 'dry-mode':
$('.heat-icon-parent.clicked').css('background', 'linear-gradient(141.67deg, #00BCB5, teal)');
break;
case 'auto-mode':
// Inherit the color of the last clicked mode for auto-mode
var lastClickedModeColor = $('.heat-icon-parent.clicked').css('background');
$('.heat-icon-parent.clicked#auto-mode').css('background', lastClickedModeColor);
break;
default:
// Default background color
$('.heat-icon-parent.clicked').css('background', 'linear-gradient(141.67deg, #56aff3, #1c71c8)');
break;
}
}
CSS
.heat-icon-parent.clicked,
.heat-icon-parent.clicked#auto-mode {
border-radius: var(--br-21xl);
background: inherit;
background: linear-gradient(141.67deg, #56aff3, #1c71c8);
box-shadow: 0 2px 2.1px rgba(0, 0, 0, 0.4),
-1px -1px 2px rgba(0, 0, 0, 0.5) inset,
1px 1px 2px rgba(255, 255, 255, 0.64) inset;
display: flex;
flex-direction: row;
color: var(--white);
}