Dynamic background with CSS? How to do it?

Asked

Viewed 219 times

1

I have a question, it is possible to exchange background as a condition?

Example:

function A() {
  if (meuArray.length === 7) {
    eu quero background verde
  } else {
    background azul
  }
}

At first I’m making a card game called Black Jack or 21 in Brazil, and so far I haven’t found a viable way to by background or a CSS in the array that contains the values of each card.

Would anyone have a tip or how to do it? Thank you

  • 1

    Yes. Use document.querySelector("#your-element").style.background = "#F00" or document.querySelector("#your-element").style.setProperty("background", "url('your-image.png')")

1 answer

1

Good afternoon you can use the card numbers and assemble an array with them and when using them just call the position of the array.

example:

var cartas = [];
cartas[0] = '#f333dd';
cartas[1] = '#ffffff';
cartas[2] = '#000000';
cartas[3] = '#f23f5d';

....

$(element).attr('style','background-color:' + cartas[2] + '');

I hope this code helps you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.