Currently exploring the realm of JavaScript, I am tackling a challenge with adding a class to an element based on its background color. Specifically, I aim to add a class to elements with a background color set to rgb(32, 44, 62)
. However, my current implementation applies the class to all elements, not just those with the specified background color. I have shared a test of the code below and any insights would be greatly appreciated.
https://codepen.io/tyearyean/pen/PoWvLwO
var basicBGColor = $('section.content_basic').css('background-color');
Javascript
var basicBGColor = $('section.content_basic').css('background-color');
$('section.content_basic').each(function() {
if (basicBGColor == 'rgb(32, 44, 62)') {
$(this).addClass('goldBorder');
};
});