James B James B - 1 month ago 7
Java Question

JQuery changing colour of Div

My Javascript/jQuery is a little rusty so this may be glaringly obvious but, I have a div and want it to change colour when clicked which I can do using

$(".MyClass").on("click", function()
{
$(this).css("background", "#0077af");
});


I have now tried to incorporate this into a boolean if statement so that if clicked it changes colour, then if clicked again it changes back, so on and so forth, however it now does nothing.

<script>
boolean clicked = false;
$(".League").on("click", function()
{
if(clicked == false)
{
$(this).css("background", "#0077af");
clicked = true;
}
else
{
$(this).css("background", "#00aeff");
clicked = false;
}
});
</script>


My knowledge of Java is stronger than Javascript and knowledge of jQuery very limited so nothing too complex please :)

Answer

change your variable initialisation statement

boolean clicked = false;

to

var clicked = false;

and best to use "backround-color" rather than "background"

Comments