event.target
returns the DOM element an action was performed on. How do I get a specific child element from the output?
Here's my form (with Angular markup):
<form (ngSubmit)="onSubmit($event)" id="kekpek"> <div class="form-group"> <label>Example file input</label> <input type="file" class="form-control-file" #fileupload [(ngModel)]="myFile" name="myFile" (change)="fileChange($event)"/> </div> <button type="submit" class="btn btn-success">Submit</button> </form>
It fires the function:
onSubmit(event) { console.log(event.target); }
Which output the entire form. I need to access the input
element in the onSubmit()
function.
Select the parent element whose child element is going to be selected. Use . querySelector() method on parent. Use the className of the child to select that particular child.
You can use the documentElement.querySelector()
method.
function onSubmit(event){ console.log(event.target.querySelector('input')); }
You can use any valid CSS query to get the element you need
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