-1

When I define a code to click and change that element again, I want the element to return to the first state with another click

I wrote another click but it didn't work The toggle didn't work properly either I want css to appear when clicked and css to be removed when clicked again

$("#green").click(function() {
  var audio = new Audio('sounds/green.mp3');
  audio.play();
  $("#green").css("background-color", "gray");
  $("#green").css("border", "20px solid white");
});

$("#green").click(function() {
  var audio = new Audio('sounds/green.mp3');
  audio.play();
  $("#green").css("background-color", "green");
  $("#green").css("border", "0px");
});

1

2 Answers 2

1

Just set up the two different styles as classes and one click event that toggles between them. You just need to be careful that the secondary style is written with a selector that is more specific than the default so that it will override when applied.

$("#green").click(function() {
   //Toggle will work if the classes are set up correctly.
   this.classList.toggle("clicked");
});
/* Set up a default style  */
#green {
  background-color:gray;
  border:20px solid blue;
}

/* Define secondary style to be toggled on/off */
/* This selector needs to be more specific than the default (ID) selector,
   so it will be a Class.ID selector.  */
#green.clicked {
  background-color:green;
  border: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<div id="green">This is the content area</div>

0

I wrote another click but it didn't work

I'd wager that both of the click handlers worked just fine, they simply cancel out the overall result because they both run on every click.

You only need one click handler, it just needs to conditionally perform one operation vs. another. One way to maintain that condition is to store in some global state which operation was last performed. For example:

var isClicked = false;
$("#green").click(function() {

  var audio = new Audio('sounds/green.mp3');
  audio.play();

  if (isClicked) {
    $("#green").css("background-color", "green");
    $("#green").css("border", "0px");
  } else {
    $("#green").css("background-color", "gray");
    $("#green").css("border", "20px solid white");
  }

  isClicked = !isClicked;
});

Each click does three things:

  1. Play the audio
  2. Conditionally change CSS one way or another
  3. Toggle the isClicked so that the next click will choose the other condition

We could take this a step further though. Since what you're toggling are CSS styles, use CSS (instead of JavaScript) to define those styles. For example, suppose you have these styles:

#green {
  background-color: green;
  border: 0px;
}

#green.active {
  background-color: gray;
  border: 20px solid white;
}

Then all you'd need to toggle is the active class on the element. And conveniently enough, jQuery has a method to do that without having to track state in a variable. So your JavaScript code simplies to:

$("#green").click(function() {
  var audio = new Audio('sounds/green.mp3');
  audio.play();
  $("#green").toggleClass("active");
});
1
  • 1
    Why not just set up a default class and an "active" class and then just use the .toggle() method to switch between the two. Having to write a conditional statement and apply inline styles is a very outdated approach. Commented Jun 24 at 21:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.