Skip to content
Advertisement

When is an event.target.value not a string?

I came accross value = String(event.target.value || "") when a textinputs keyup/keydown event is fired.

But i’m not sure when the event.target.value is not a string? Is this possible? When is something else passed off as an event.target.value?

Advertisement

Answer

If the event.target element is not an input type element, it will not have a value property. For example, if I click a div then event.target is a div which does not have value.

Wrapping event.target.value || '' in String() is not necessary as it will always be either value (which is always a string or undefined) or the empty string in the case that value is undefined.

See this fiddle for a demonstration.

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