Skip to content

undefined value on input and other elements #4467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
PatrickG opened this issue Feb 26, 2020 · 6 comments · Fixed by #15057
Closed

undefined value on input and other elements #4467

PatrickG opened this issue Feb 26, 2020 · 6 comments · Fixed by #15057
Labels
bug compiler Changes relating to the compiler

Comments

@PatrickG
Copy link
Member

Describe the bug
The value prop behaves differently in various situations.

Suppose we have a variable let value = undefined.

If you use it on an input element with two-way binding <input bind:value />, the value is not set (this is the correct behavior, it got fixed in #3430).
If you use it on an input element like this: <input {value} />, the input elements value property is set to the string 'undefined' (not the attribute).
If you use it on any other element, like: <option {value} />, the value attribute is set to "undefined".

To Reproduce
https://svelte.dev/repl/9a8cf877df9a47948eb0cd3b04ea8372?version=3.19.1

Expected behavior
I think the value attribute (without two-way binding) should behave like the disabled attribute in the repl.

Severity
low

Additional context
You can get around it with <input value={value || ''} />

@Conduitry
Copy link
Member

Is this the same as #3569?

@tanhauhau tanhauhau added the dom label Mar 18, 2020
@PatrickG
Copy link
Member Author

Not really. Its more about the inconsistency when not using two-way binding vs using two-way binding.

@dgellow
Copy link

dgellow commented Oct 27, 2020

You can get around it with <input value={value || ''} />

That doesn't work as expected when using <input type="submit value={value || ''} />. That results in a submit button with no text instead of the expected "Submit".

Edit: it's a detail, just worth pointing out that this workaround has some unexpected effect.

@stale
Copy link

stale bot commented Jun 26, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale-bot label Jun 26, 2021
@stale stale bot removed the stale-bot label Jun 26, 2021
@pngwn pngwn added compiler Changes relating to the compiler bug stale-bot and removed feature: attributes temp-stale labels Jun 27, 2021
@stale stale bot removed the stale-bot label Jun 27, 2021
@PatrickG
Copy link
Member Author

This is fixed in Svelte 5

@theodorejb
Copy link
Contributor

theodorejb commented Jan 17, 2025

This is fixed in Svelte 5

It doesn't seem to be fixed completely. I just ran into this issue with Svelte 5.19.0.

When an input element is initially rendered, if the value attribute is undefined, then the attribute isn't added to the DOM (as expected). However, if the value attribute is not undefined, and then the bound value changes to undefined, then the input unexpectedly displays "undefined". This behavior is unexpected, and inconsistent with two-way bindings, where undefined is never displayed. It is also inconsistent with null, which is never displayed in either case.

Reproduction (click the "Toggle value" button twice): https://svelte.dev/playground/9a8cf877df9a47948eb0cd3b04ea8372?version=5.19.0#H4sIAAAAAAAACn1RQU7DMBD8ymrFoZWASByDm4obdxAXzCFNtq2FWVvxpqGK8ndsp4hKQE_WzszOzMojcv1BWOIjWetgcJ1t8Rq3xlLA8nVEOfpEJyDiJ_GD97fhQFYStqkD_YU3joVYog2q0HTGS6VZiyUBoSCwgqsgtdBieR_xyGx7bsQ4BnG7naWX2vaRhDFxWk47-VlDzy1tDVMLJWhMoMZkI5NmVfzEsdrfVdkJTIDxSTrDu0XSLydVRC5rfG6mNr1Iio8nrzTOk0Zw3FjTvK_Gs15T9ZwHOKRJFbO4SuH-29Ow7wUGI3tQjWupGjVmeXSKBSaNsUImYOGYbob6CBvDbWy4LGcfNXucb0FxyT_tl_-FyOAuhPxazUnxI4U-BUvp4tVv0xfc95kSMQIAAA==

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug compiler Changes relating to the compiler
Projects
None yet
7 participants