I understand that pyscript can be used as a client side scripting language. Is there a way to interact with the DOM by using CSS selectors, such as we have in javascript.
Something like :
nav_bar = get_element(".nav-bar")
CodePudding user response:
Yes, you can call JavaScript functions and access globals from Python. Import the js namespace:
import js
or
from js import document
Then you can call functions like this:
<body>
<div id="msg">Loading page ...</div>
<py-script>
from js import document
msg = document.getElementById("msg")
msg.innerHTML = 'Hello world'
</pyscript>
</body>
The function get_element()
is not a standard JavaScript function. It is a function located in libraries such as Telerik. Normally you can call those functions from Python provided they are not doing something special with namespaces and the library is loaded before PyScript. You can use normal JavaScript functions from Python to set CSS styles such as:
document.getElementById("msg").style.color = "blue"