Edit custom style using javascript


Hello, I’m using a .css file in my header.
Inside the .css there’s a line:

[data-filepond-item-state='processing-complete'] .filepond--item-panel {
  background-color: #369763;

Do I want to give my users the ability to choose the background color, how I can change that color using javascript?

I tried to do:

  	const el = document.querySelector(myelement);
		el.style.position = "absolute";

But not sure where to add all that line



I think what you’re looking for is el.style.backgroundColor


The most versatile way would be with CSS variables:

:root {
  --background-color: #369763;  

[data-filepond-item-state='processing-complete'] .filepond--item-panel {
  background-color: var(--background-color);

Then you can do something like

  Choose background colour:
  <input type="color" id="color-picker">

colorPicker.addEventListener('change', () => {

Unfortunately there’s no IE support and probably never will be… @gandalf458’s solution will work in all browsers though.


Hello @gandalf458, thank you for your answer.

But that will not affect the other background-colors in the .css?
How this:

is referencing to only that background color?



Thank for your answer @m3g4p0p

But in your example are you using :root?
What does that do?

In my case, I can’t edit the.css file


That’s what the el. is for.


No, that’s not what I mean.

This is a piece of the .css file:

.filepond--item-panel {
  background-color: #64605e;

[data-filepond-item-state='processing-complete'] .filepond--item-panel {
  background-color: #369763;

[data-filepond-item-state*='invalid'] .filepond--item-panel,
[data-filepond-item-state*='error'] .filepond--item-panel {
  background-color: #c44e47;

How is this:

Referencing only to this background:

[data-filepond-item-state='processing-complete'] .filepond--item-panel {
  background-color: #369763;



I had rather assumed that myelement was pointing to the element you wanted to change.


But that element also is using the another backgrounds


It matches the root element of the document; this makes sure that the variables cascade down to every other element, and are directly accessible with JS via document.documentElement.

In this case you can still override the styles manually by appending your own… for example:

var setBackgroundColor = function (selector, color) {
  var style = document.createElement('style')
  style.textContent = selector + '{background-color:' + color + ';}'

  '[data-filepond-item-state="processing-complete"] .filepond--item-panel', 

(PS: If you do this a lot this will however pile up style sheets… so you might also keep track of the added styles and just replace them where necessary.)


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.