cambiar el background color de un span in javascript

To change the background color of a span element using JavaScript, you can set the style.backgroundColor property of the element.

Here's an example code snippet that demonstrates how to do it:

index.tsx
const element = document.getElementById('mySpan');
element.style.backgroundColor = 'red';
90 chars
3 lines

In this example, the background color of the span element with id="mySpan" is turned to red.

Alternatively, you can also apply a CSS class to the span element using the classList.add() method and define the background color in the CSS.

Here's an example code snippet that demonstrates how to do it:

index.tsx
const element = document.getElementById('mySpan');
element.classList.add('red-background');
92 chars
3 lines

And in your CSS file:

.red-background {
  background-color: red;
}
45 chars
4 lines

related categories

gistlibby LogSnag