Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get child element from event.target

Tags:

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.

like image 746
mikebrsv Avatar asked Jan 29 '18 04:01

mikebrsv


People also ask

How do you target parent to child in Javascript?

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.


1 Answers

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

like image 191
Chirag Ravindra Avatar answered Sep 19 '22 15:09

Chirag Ravindra