DOM manipulation with PyScript

This might be one of those “We could do it, but why would we want to” questions (or maybe it has already been addressed, and I missed it), but here goes. I know that PyScript has some limited ability to interact with the DOM without calling JS functions directly, e.g. using Element(‘id’).write() instead of document.getElementByID().write. Is there a way within PyScript to alter CSS? In other words, is there a “pure” PyScript equivalent to:

from js import document
. . .
document.getElementById('id_name").style.backgroundColor = "red"

For fun, I tried Element('id').style.backgroundColor = "red", but it doesn’t work. Does PyScript have this functionality and, if not, are there plans to add it?

1 Like

Can’t speak to what’s coming next for PyScript, but if you want do something like your second example, the syntax is:

Element('id').element.style.backgroundColor = 'red'

The PyScript Element class has an attribute element which represents the actual DOM element. (See Pyscript.py)

2 Likes

Of course. I guess if I had actually explored the Element class a little more I could have figured it out. As always, thanks for the help.

1 Like