How to get input from ion-searchbar?

Issue

It is super easy problem but I just can’t seem to figure this out (And yes I have read the documentation).

I am trying to get the input user puts in the ion-searchbar (in Ionic v4) after they the press search and put in a const/let.

Mah HTML

<ion-searchbar showCancelButton="focus" class=""></ion-searchbar>

I don’t know how I should write the TS for this.

Thanks in advance :{)

Solution

  • Use (search) event to call your function. This event is fired when the user will click on the search button provided by the ion-searchbar.

  • To get the value entered in the searchbar, use $event.target.value which gets the value field of the tag which in this case is <ion-searchbar>

    <ion-searchbar
        showCancelButton 
        searchIcon="search" 
        animated
        cancel-button-icon
        (ionCancel)="hideSearch()"
        (search)="yourSearchFunction($event.target.value)" 
        placeholder="Search Some Values">
    </ion-searchbar>
    

To listen to changes as user types in the search bar:

    <ion-searchbar
        ... 
        (ionInput)="yourInputChangeFunction($event.detail.value)">
    </ion-searchbar>

Answered By – Pankaj Sati

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published