8

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

2

3 Answers 3

9

//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);
Sign up to request clarification or add additional context in comments.

1 Comment

I am getting error -ERROR TypeError: Cannot read property 'dispatchEvent' of null
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)"/>

Comments

1

JS code:

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

Happy Coding!!!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.