question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] pys-onClick attribute does not work, when element has no id

See original GitHub issue

Describe the bug When you whant to use the pys-onClick attribute to define a python function as the click listener, and the html element has no id, it does not work.

Error:

pyodide.asm.js:14 Uncaught (in promise) PythonError: Traceback (most recent call last):
  File "/lib/python3.10/asyncio/futures.py", line 201, in result
    raise self._exception
  File "/lib/python3.10/asyncio/tasks.py", line 232, in __step
    result = coro.send(None)
  File "/lib/python3.10/site-packages/_pyodide/_base.py", line 500, in eval_code_async
    await CodeRunner(
  File "/lib/python3.10/site-packages/_pyodide/_base.py", line 351, in run_async
    coroutine = eval(self.code, globals, locals)
  File "<exec>", line 1, in <module>
  File "<exec>", line 141, in element
JsException: SyntaxError: Failed to execute 'querySelector' on 'Document': '#' is not a valid selector.

To Reproduce Using:

  • Chrome Version 101.0.4951.67
  • Firefox Version 100.0 (64-bit)

Use this html and click on the button:

<!DOCTYPE html>
<html lang="en">
<head>
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>

<body>
	<py-script>
from js import console
def clicked(*args, **kwargs):
	console.log("clicked")
	</py-script>
	
	<button pys-onClick="clicked">Click me!</button>
	
</body>
</html>

Expected behavior The message “clicked” should be logged to the console. With an id it works:

<!DOCTYPE html>
<html lang="en">
<head>
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>

<body>
	<py-script>
from js import console
def clicked(*args, **kwargs):
	console.log("clicked")
	</py-script>
	
	<button id="click-button" pys-onClick="clicked">Click me!</button>
	
</body>
</html>

EDIT: Added chrome, firefox version number

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:35 (17 by maintainers)

github_iconTop GitHub Comments

4reactions
yannickfunkcommented, May 16, 2022

@fpliger I created a PR

2reactions
pauleverittcommented, May 13, 2022

I’m actually getting this with hello_world.html from a checkout. The checkout worked a few days ago. I can’t spot any changes in the commits that might have caused it. Still pointing to 0.20 of Pyodide.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I call a python script when clicking a button (using ...
This only works if the button with the pys-onClick has an id. The id provided to the pyscript.write function is the id of...
Read more >
Intro to PyScript: Run Python in the browser - LogRocket Blog
The pys-onclick attribute accepts the function name handle_click , which will be the function that runs when the button is clicked. Next, we ......
Read more >
HTML onclick Event Attribute - W3Schools
Definition and Usage. The onclick attribute fires on a mouse click on the element. Browser Support. Event Attribute.
Read more >
The 10 Most Common JavaScript Issues Developers Face
If you need help figuring out why your JavaScript isn't working, consult this list of the 10 most common JavaScript issues from a...
Read more >
Build a Todo App With PyScript - Bas codes
We can run native Python code in the browser using PyScript. ... thing here is the binding of the onClick event to a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found