Perhaps he should have been
$("#spanText").attr('style', 'background-color:gray');
This may work, but has some problems:
- It is preferable to change the
style property instead of the style attribute. - It will replace all previously installed inline styles.
Then, if you are using jQuery, it is better to use the css method:
$("#spanText").css('background-color', 'gray');
But the style property is useful in vanilla-js:
document.getElementById("spanText").style.backgroundColor = 'gray';
Orientol
source share