Essentially, I want to trigger the input button in the page using TypeScript
Asked
Active
Viewed 1.8k times
3 Answers
7
//its no different than doing it in vanilla JS
let elem = document.getElementById('submitBtn');
let evt = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window
});
elem.dispatchEvent(evt);
Amit Soni
- 3,136
- 5
- 28
- 49
Thomas Lang
- 101
- 6
-
1I am getting error -ERROR TypeError: Cannot read property 'dispatchEvent' of null – Techdive Feb 26 '19 at 12:20
4
Use @ViewChild as follows in .ts
@ViewChild('fileInput') fileInput: ElementRef;
let inputElement: HTMLElement = this.fileInput.nativeElement as HTMLElement;
inputElement.click();
In your .html,
<input #fileInput type="file" ng2FileSelect (onFileSelected)="fileUpload($event)"/>
Parinda Rajapaksha
- 2,556
- 1
- 29
- 37
0
JS code:
document.getElementById('mat-checkbox-1-input').click();
Happy Coding!!!
sweetnandha cse
- 349
- 4
- 7