Skip to content

py-on* event handler code is being executed at load time #685

@JeffersGlass

Description

@JeffersGlass

Checklist

  • I added a descriptive title
  • I searched for other issues and couldn't find a solution or duplication
  • I already searched in Google and didn't find any good information or help

What happened?

Py-event handler code being executed at page-load, which differs from Javascript in which the code is only executed when the event is received. i.e, in JS one would write <button onClick="foo()">, but in PysScript one would currently write <button py-onClick="foo"> This makes it the syntax for passing arguments to event functions different from Javascript in an unintuitive way.

Consider the following examples, first Javascipt (JSFiddle), then in PyScript (JSFiddle), which I think the user would expect to do the same thing. (You made need to re-save them to get the JS to run):

// Javascript
<button class="submit" id="hello" type="submit" onClick="sayHello('Jeff')">Say Hi</button>
<div id= "greeting"></div>
<script>
    function sayHello(name) {
        document.getElementById('greeting').innerHTML = "Hello, " + name
    }
</script>
//PyScript
<button class="submit" id="hello" type="submit" py-onClick="sayHello('Jeff')">Say Hi</button>
<div id="greeting"></div>
<py-script>
    def sayHello(name):
        Element("greeting").write(f"Hello, {name}")
</py-script>

The problem is, these two do not do the same thing. The py-onClick code is executed immediately on page-load, as the value of the py-on* attribute is evaluated directly.

After the page loads and the PyScript runtime initializes:
image

Proposed Solution

I suggest changing the behavior of py-on* events to match the Javascript syntax.

What browsers are you seeing the problem on? (if applicable)

No response

Console info

No response

Additional Context

This was spotted by user mark.d.lattimore on the Anaconda forums.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Closed

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions