I'm very new to JavaScript and jQuery. I want to make the white button at the top change color after these list of colors is pressed. For example, when the pink button is pressed, the white button turns pink, and then when the purple button is pressed, it changes to purple:
https://i.sstatic.net/q4zA0.png
I've managed to get it to work when the pink button is pressed using this function that selects the ids of the li element and the .addClass() method.
//When clicking diff colours
$("ul").on("click", "li", function(){
if ($("li").is("#p")) {
$("button").addClass("pink");
} else if ($("li").is("#pu")) {
$("button").addClass("purple");
}
color = $(this).css("background-color");
});
However, using else if doesn't seem to work when, for example, pressing the purple button.
Here is the HTML snippet:
<div id="colorSelect">
<ul>
<li class="pink" id="p"></li>
<li class="purple" id="pu"></li>
<li class="red" id="r"></li>
<li class="blue" id="b"></li>
<li class="green" id="g"></li>
<li class="orange" id="o"></li>
</ul>
</div>
