1

I want to add css that will change every time I click. But I couldn't find anything like this in jquery. How can I do this using jquery?

I want you to click on the background color every time. I know how to do this with toggleClass. But I want to do by adding css. The background turns red when the button is clicked. I want it to change when clicked again. I have to do this using .css, not ToggleClass.

$(function() {
  var a = 0;
  $('.mob-channel-title').click(function() {

    $(".content").css("background", "red");

  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="mob-channel-title">Click me</div>
<div class="content">Content here</div>

1
  • Slight clarification here. Are you looking to change the color between red and the default background color each time? Or are you looking to change it between an array of colors? Or random colors? Or... Commented Jul 26, 2020 at 6:50

3 Answers 3

3

If I understand correctly, you want to toggle the background between "red" and the default background; in this case, a simple approach would be to replace the a variable with a new background variable that tracks the background state to achieve what you require:

$(function() {
  
  var background;
  
  $('.mob-channel-title').click(function() {

    // Compute next background based on current background
    background = background === "red" ? "" : "red"
    
    // Apply newly updated background to .content
    $(".content").css("background", background);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="mob-channel-title">Click me</div>
<div class="content">Content here</div>

Sign up to request clarification or add additional context in comments.

Comments

1

ToggleClass is the preferred way, but try this

$(function() {
  let a = 0;
  const colors = ["red", "green", "blue"];
  $('.mob-channel-title').on("click", function() {
    $(".content").css("background", colors[a++]);
    if (a >= colors.length) a = 0;
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="mob-channel-title">Click me</div>
<div class="content">Content here</div>

Comments

0

Wasn't sure if you meant that you wanted the color to toggle from white to red or if you wanted a new color each time. So here's both using .css

$(function() {
  var a = 0;
  $('.mob-channel-title').click(function(e) {
    let color = $(".content").css('background');
    $(".content").css("background", color.includes('rgb(255, 0, 0)') ? 'white' : 'red');

  });
});

$(function() {
  var a = 0;
  $('.random-color').click(function(e) {
    const r = parseInt(Math.random() * 255);
    const g = parseInt(Math.random() * 255);
    const b = parseInt(Math.random() * 255);
    $(".content").css("background", `rgb(${r}, ${g}, ${b})`);

  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="mob-channel-title">Click me</div>
<div class="random-color">Random Color</div>
<div class="content">Content here</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.