0

I have a div and ion-input #fileInput type="file" accept="image/*" id="fileInput" [(ngModel)]="imageFilePath" (ionChange)="imageFilePath_change($event)" ></ion-input>

How to simulate the click on the ion-input component using the div?

my html code is:

<div (click) = "fileInput.click()">
    <img src="assets/img/camera_button.png" [ngStyle]="{'position': 'fixed', 'top': '30vw', 'left': '32vw', 'height': '30px', 'color': '#0080FF'}">
    <ion-input #fileInput type="file" accept="image/*" id="fileInput" [(ngModel)]="imageFilePath" (ionChange)="imageFilePath_change($event)" ></ion-input>
    <span [ngStyle]="{'position': 'fixed', 'top': '32vw', 'left': '42vw', 'color': '#0080FF'}">{{ 'addMorePhotosBtn' | translate }}</span>
</div>

the error is:

TypeError: jit_nodeValue_20(...).click is not a function
6
  • <ion-input (click) ... ? Commented Jun 13, 2018 at 13:15
  • no i simulate the click of ion-input in a div Commented Jun 13, 2018 at 13:16
  • Your #fileInput so ion-input hasn't function named click Commented Jun 13, 2018 at 13:17
  • Why do you need the input to be clicked? Why not just call the component method from the original click event handler? Commented Jun 13, 2018 at 13:21
  • please you can write a simple example please? Commented Jun 13, 2018 at 13:24

1 Answer 1

1

Here is an my working Example for an file while clicking div.

Html File

 <ion-list>
       <ion-item (click)="onImageClick()">
           <div>
              <ion-icon ios="ios-add-circle" md="md-add-circle"></ion-icon>
                  Add Files
              </div>
       </ion-item>
   </ion-list>

Typescript File

public onImageClick() {

    let input = document.createElement('input');
    input.setAttribute('type', 'file');
    input.setAttribute('accept', 'image/*'); //you can change also file type as **'file/*'**
    input.setAttribute("multiple", ""); // If you dont want multiple select file pls remove this line

    input.addEventListener('change', (event: any) => {      
      let fileList: File[] = event.target.files;
      console.log("File List Object Value",fileList);
    });

    input.click();
  }

Worked perfect for me tested.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.