How to modify button attribute with Pyscritp #1206
-
Hello. I would like to know how a button can be enabled or disabled using pyscript. The code would look something like this, but using ·setAttribute" doesn't work. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Hello @joseludenia you need to do def enabled():
button = Element("but").element.setAttribute("disabled", True) |
Beta Was this translation helpful? Give feedback.
-
Here's a full working code that you can use: <!DOCTYPE html>
<html lang="en">
<head>
<title>Enable Disable Buttons</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="https://pyscript.net/releases/2022.12.1/pyscript.css" />
<script defer src="https://pyscript.net/releases/2022.12.1/pyscript.js"></script>
</head>
<body>
<button id="button-1" py-click="toggle1()">Button 1</button>
<button id="button-2" py-click="toggle2()">Button 2</button>
<py-script >
def toggle1():
button1 = Element("button-1").element
button2 = Element("button-2").element
if button2.disabled:
button2.removeAttribute("disabled")
button1.setAttribute("disabled", True)
def toggle2():
button1 = Element("button-1").element
button2 = Element("button-2").element
if button1.disabled:
button1.removeAttribute("disabled")
button2.setAttribute("disabled", True)
</py-script>
</body>
</html> The code is a bit rough, but should give you an idea of how to do what you wish. Also in py-script you can either write your python code between
This is useful if you want to use any help from your IDE (VsCode for example) such as code highlighting and autocompletion. Hope this helps 😄 |
Beta Was this translation helpful? Give feedback.
Here's a full working code that you can use: