Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bgColor not updating via JavaScript

Tags:

javascript

Why does the background color of the div not update?

Tagger.prototype.mouseDown = function(event) {
  this.element.style.posLeft = event.clientX;
  this.element.style.width = 200 + "px";
  this.element.style.height = 200 + "px";
  this.element.style.position = "absolute";
  this.element.style.zIndex = 10;
  this.element.style.bgColor = "yellow";
  console.log(this.element);
  console.log(this.element.style.bgColor);
}

The console output is:

<div id="tagbox4" class="tagbox" style="width: 200px; height: 200px; position: absolute; z-index: 10;">
yellow

I can select the div by inspecting the element in the browser, but it has no background color, even though the console says it is "yellow".

like image 747
Rose Perrone Avatar asked Mar 15 '26 16:03

Rose Perrone


1 Answers

It is not bgColor, that is an old outdated way of setting background colors when CSS was not popular.

You want to set the CSS property background-color. When you set it with JavaScript, you remove the dash and use camelCase. So you want to set backgroundColor.

this.element.style.backgroundColor
like image 143
epascarello Avatar answered Mar 17 '26 05:03

epascarello



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!