c-smile / sciter-js-sdk

Sciter.JS - Sciter but with QuickJS on board instead of my TIScript
BSD 3-Clause "New" or "Revised" License
1.64k stars 97 forks source link

Can't change element styles from JavaScript #5

Closed GirkovArpa closed 3 years ago

GirkovArpa commented 3 years ago

I'm trying to implement the 7GUIs benchmarks with SciterJS, but I've run into the problem that I can't seem to change the style.

In this code I'm trying to make the input background color red:

'use strict';

const inputs = [...document.querySelectorAll('input[type=text]')];
const [inputStart, inputReturn] = inputs;

inputs.forEach((input) => {
  input.addEventListener('change', () => {
    const [day, month, year] = input.value.split('.');
    const date = new Date(year, month, day);

    if (date == 'Invalid Date') {
      // trying to change background color to red

      //input.style.backgroundColor = 'red'; // cannot set property 'backgroundColor' of undefined
      //input.classList.add('invalid'); // cannot read property 'add' of undefined
      //input.setAttribute('style', 'background-color: red'); // not a function
    } else {

    }
  });
});

I don't understand why .setAttribute isn't working since it's marked as implemented here.

I also tried input.className = 'date invalid' but I get the error that there isn't a setter for this property.

Is it possible to modify an element's styles in JavaScript using SciterJS yet?

jsprog commented 3 years ago

All the problems mentioned above are tested to work. Likely you're not waiting for the DOM to get ready.

Sciter.js is new and some features are being added by time. I planned to rise an issue about element.style.setProperty but it's available as a string property instead of a function and likely it'll land very soon.

Note: you could still patch document.createElement and replace element.style.setProperty with your own implementation using element.style[propertyName], but pay attention to differences (e.g: backgroundColor instead of background-color)

GirkovArpa commented 3 years ago

You're right, I tried document.querySelector('body').style.backgroundColor = 'red'; at the top of the script and it worked.