Please see this minimum example https://codepen.io/rockmandash/pen/Rzwowd
The code is this:
<div class="cool">
<input type="checkbox" value="checkbox1">
<input type="checkbox" value="checkbox2">
<input type="checkbox" value="checkbox3">
</div>
document.querySelector(".cool").addEventListener("click", event => {
// In React World
// How do I check if event.target is typeof HTML.input Element?
console.log(event.target.value);
});
I'm asking this question because I have thousands of input elements, in react, I will have to create thousands callback function.
So I move my function up to their parent, but then I have to validate if I am current clicking an input element.
Otherwise, I could potentially set the wrong event.target.value.
To check the element type pf an event target, using the is() method.
Using onChange={({ target: { value } }) => setInsertedTitle(value)} you are passing the current target value as a parameter. It is because onChange generates an Event, and you access the value by event. target.
What is the type of event in TypeScript? Events supported are: AnimationEvent , ChangeEvent , ClipboardEvent , CompositionEvent , DragEvent , FocusEvent , FormEvent , KeyboardEvent , MouseEvent , PointerEvent , TouchEvent , TransitionEvent , WheelEvent .
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 .
if (event.target instanceof HTMLInputElement)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With