Here is the code I've been working on:
function captureTraits(trait) {
$("#"+trait).on("click", function() {
alert($(this).css('backgroundColor'));
if (convertToHex($(this).css('background-color')) != selectedColor) {
$("#"+trait).css("background-color", selectedColor);
// Toggle highlight if not already highlighted.
} else {
$(this).css("backgroundColor", defaultColor);
}
})
}
I am attempting to create a toggle effect for highlighting a div when clicked by the user. Instead of using boolean toggles for each div, I want to retrieve the background color of the div dynamically. To achieve this, I require a convertToHex(rgb)
function. Despite finding several similar functions on Stack Overflow, none of them have worked for me. The alert()
I included to display the jQuery output showed rgba(0,0,0,0)
. I tried adjusting a regex pattern I found like so:
var rgb = rgb.match(/^rgba((\d+),\s*(\d+),\s*(\d+))$/);
However, this modification resulted in a TypeError: rgb is null
.
Any assistance you can provide would be greatly appreciated!