Skip to content
Advertisement

Give empty input values a value only when they are empty/null, when a response function is executed in JavaScript

Otherwise, I want their original value.

Basic desired outcome: For this example, type a name to see the result. Erase the name and I want it to say “[empty]” instead of the name (in the text below the field). If I type the name again it should show the name again.

I am using “span” in a larger program so I used it in this example just in case, regardless the result should be the same.

EDIT: nevermind, this is false. The result is not the same. I need it to work when “span” is in the declared variable.

I am having a huge issue here. if/else statements are not working and I have tried all variations of ternary arguments.

I got this working halfway by doing the following after declaring nameHere, however, it would not return to having the inputted name value when it was entered:

JavaScript

Here is some sample code of what I have right now:

JavaScript
JavaScript

Advertisement

Answer

Please run following snippet. Once you type a name, it will be printed in the result div, once you erase EMPTY will be shown instead.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement