8

Essentially, I want to trigger the input button in the page using TypeScript

RedOne
  • 115
  • 1
  • 1
  • 5

3 Answers3

8

//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,216
  • 6
  • 31
  • 50
Thomas Lang
  • 111
  • 6
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,963
  • 1
  • 36
  • 40
1

JS code:

document.getElementById('mat-checkbox-1-input').click();

Happy Coding!!!

sweetnandha cse
  • 705
  • 7
  • 16