PricingBlog

Unexpected behaviour in file input -> set variable?

  • neville9288-1282302558450356284

    Neville

    1 year ago

    I'm seeing this behaviour with a file input element and setting a variable:
    - On the input event, I log to console: event -> target -> get(files) -> get([0])
    - I then set the variable to exactly the same: event -> target -> get(files) -> get([0])
    - I then log the variable to the console
    On the first input, the event data is as expected and the variable is set to the event data

    On the second input, the event data is as expected (the new file) but the variable is not updated.

    The only way I can work around this is to set the variable to nul before assigning it the event data. Not sure if I'm using the correct event (input)?

    I'm attaching a screenshot of the console log, and I have a little demo project that demonstrates the behaviour if anyone wants to take a look.
    Thanks!
    1282307682551861340-Screenshot_2024-09-08_at_6.52.01_AM.png
  • tomthebigtree-1282331896570839040

    Tom Ireland

    1 year ago

    Yeah, you're correct - you need to clear the variable first, set to null before you set the variable with the new value if you're updating it. input works as does onchange. I've only used the latter so cannot confirm the behaviour of input for files.