getElementById - ion-input

Don’t ever type the word “document” in an Ionic app. Use property binding with ngModel or Angular’s FormControl to bind the element to a property in the controller instead of direct DOM access.

1 Like

Thanks for answer:
i am new in ionic apps:

<ion-item *ngFor="let item of itemDetail; let i=index ">

<input id=rcv{{i}} [(ngModel)]=“item.inv_Rcvamt” class=“clix-input-1s” maxlength=“100”>

<ion-checkbox id=“check1” [(ngModel)]=“item.check_status”>

above my html code: [this is a dynamic data from my server]

Now what should I want?

  1. when I type an value in ion-input
  2. i want this value from ion-item by using 'ID" [id=rcv{{i}} ] because i am not able to get value dynamically from ‘[(ngModel)]’
  3. i use this: var my_Rcvpmt1 = (document.getElementById(‘amtRcvt’)).value;

but i am getting value 'undefined"

could you please help

You should probably be using <ion-input> instead of input, but the input value should always be available in the inv_Rcvamt property (which should be renamed to conform to customary JavaScript naming conventions) of each element in your itemDetail array. In addition to document, var is another word you should never be typing. Always use let instead.

ngAfterViewInit () {

}
Solved my problem, thanks.

thx Josh, I got it work.

For anyone who wants to get it work:

=====================

<div class="form-group">
     <ion-input #search [(ngModel)]="address" name="search" >
      </ion-input>
 </div>

My .ts:

 @ViewChild("search", { read: ElementRef } ) searchElementRef: ElementRef;
  ngAfterViewInit() {
   
      //load Places Autocomplete
      this.mapsAPILoader.load().then(() => {
         let autocomplete = new google.maps.places.Autocomplete(this.searchElementRef.nativeElement, {
         types: ['geocode']
      });
   }

========================

Note: In order to get a reference of an element that hosts a component or directive you need to specify that you want the element instead of the component or directive. In this case, { read: ElementRef } is needed. Otherwise, you will get undefined.

1 Like

this last comment save my life, is the right solution, thanks.