Set localStorage value and get the value in one JS code

I am making a demo page with retool.
I am new to JS, so I might not understand the nature of JS yet.

I try to set a value to a localStorage variable.

eg) localStorage.setValue("a", "yes")

then, I try to use the set value, in one of the following lines.

eg) localStorage.values.a

I found the localStorage "a" is not set until the JS script ends.
It happens also with updating textArea value as below

var dialog = textArea_dialog.value
var backlog = textArea_backlog.value
dialog += "\n"+user_input_line
backlog += "\n"+user_input_line
textArea_dialog.setValue(dialog) //the value is not changed. I checked it with console log.
textArea_backlog.setValue(backlog)  //the value is not changed. I checked it with console log.

Is there any way to save something to a global variable and use it in the same JS script?

@mhkim Welcome to the community - you should use an event handler so you can add a key so that the key can be accessed using localStorage.values.yourKeyHere
Screen Shot 2022-05-12 at 12.44.32 PM
You can set the localStorage in an event handler like this
Screen Shot 2022-05-12 at 12.45.30 PM