Usr
Usr

Reputation: 2838

ionic 3 get text from ion-textarea after clicking a button

I have an ion-textareain which user can write some content, and I would like to get this text after a button is clicked.
This is the html:

<ion-row>
    <ion-item>
        <ion-textarea [(ngModel)]="myInput" (ionInput)="getItems($event)" placeholder="New Info"></ion-textarea>
    </ion-item>
</ion-row>

<button ion-button class="card-button" color="secondary" 
 (click)="addInfo()"> <ion-icon name="add-circle" class="icona-bottone"></ion-
 icon>Add Info</button>

I've tried doing this in my .ts file:

getItems(textarea) {
    // set q to the value of the textarea
    var q = textarea.srcElement.value;
    this.textSearch = q;
}
addInfo(){
    console.log("You wrote " + this.textSearch)
}

but it prints "You wrote undefined ". What is the correct way to get the text as a string and use it?

Upvotes: 3

Views: 7149

Answers (1)

Sampath
Sampath

Reputation: 65870

Since you have used 2-way data bind you can do as shown below.

.html

<ion-row>
    <ion-item>
        <ion-textarea [(ngModel)]="myInput" placeholder="New Info"></ion-textarea>
    </ion-item>
</ion-row>

.ts

console.log(this.myInput);//this is your textarea value

Upvotes: 10

Related Questions