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.

<select>: option is not not selected if value is an integer

See original GitHub issue

I found some strange behavior where I am not sure what part of lona may be the problem. See the following reproducer:

from lona.html import HTML, Option, Select
from lona import LonaApp, LonaView


app = LonaApp(__file__)


@app.route('/')
class MyView(LonaView):
    def handle_request(self, request):
        html =HTML(
            sel := Select(bubble_up=True)
        )
        sel.append(Option("Text 1", value="a"))
        sel.append(Option("Text 2", value="b"))
        sel.append(Option("Danger", value=3))

        while True:
            event = self.await_input_event(html=html)
            print(sel.value)

app.run(port=8080)

When surfing to the view the Text 1 Option is shown inside the <select>. When I now drop the menu down and select Text 2 b is printed to the console. Selecting Text 1 prints a, as expected. When selecting Danger, however, the value printed is a, not 3 as expected.

Frontend-to-server communication looks valid. But I noticed that my 3 get’s transferred as a string on the websocket.

I have traced this problem down to https://github.com/lona-web-org/lona/blob/5af71684d345f6b4f8bc79cd4d880971a30f2e2e/lona/html/data_binding/select.py#L122 . It seems the selection from the frontend is not represented in the corresponding Option: In line 124 of that file value is empty.

Using a str(3) or "3" instead of the integer works as expected. From a user perspective it should not matter if I put an integer or string in there. (It would actually come in handy since I could use the primary keys from my db in there 😄 )

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
maratoricommented, Oct 26, 2022

@fscherf Yeah, sure.

1reaction
fscherfcommented, Oct 23, 2022

@SmithChart: I started working on this yesterday and realized that the whole API of html.Select is quite confusing and unpractical, so i refactored the whole thing, with your issue addressed as well. Currently there is not much documentation (only in the patch description) but at this moment it’s only a proposal.

Branch: https://github.com/lona-web-org/lona/tree/fscherf/topic/refactor-select-api

@maratori, @grigolet, @laundmo: Would you mind to have a look too?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Select Option Value return Integer by default - Stack Overflow
From the spec you can see the value is a DOMString so no, you cannot get an integer by default.
Read more >
Using an integer as the initial value of a select tag - codeburst
The problem is that in HTML the select value is a string and not an integer, so even though there is a match...
Read more >
How to change selected value of a drop-down list using jQuery?
Example 1: This example uses val() function to set the option corresponding to the passed value.
Read more >
HTML option value Attribute - W3Schools
The value attribute specifies the value to be sent to a server when a form is submitted. The content between the opening <option>...
Read more >
HTMLSelectElement.selectedOptions - Web APIs | MDN
An option is considered selected if it has an HTMLOptionElement.selected attribute. Value. An HTMLCollection which lists every currently ...
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