Ionic events not firing with ion-select

Viewed 159

I want to get the value of the ion-select on-blur but the event is not firing. This is my current code :

<script type="module" src="https://cdn.jsdelivr.net/npm/@ionic/core/dist/ionic/ionic.esm.js"></script>
<script nomodule src="https://cdn.jsdelivr.net/npm/@ionic/core/dist/ionic/ionic.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@ionic/core/css/ionic.bundle.css" />
<ion-app>
  <ion-content>
    <ion-item>
      <ion-label>Filters</ion-label>
      <ion-select value="none" ionCancel="console.log('cancelled')" ionBlur="console.log('event')" ionChange="console.log('event')" ok-text="Add" cancel-text="Cancel">
        <ion-select-option value="none">None</ion-select-option>
        <ion-select-option value="sports">Sports</ion-select-option>
        <ion-select-option value="tech">Tech</ion-select-option>
        <ion-select-option value="cooking">Cooking</ion-select-option>
        <ion-select-option value="music">Music</ion-select-option>
      </ion-select>
    </ion-item>
  </ion-content>
</ion-app>

Can someone please tell me what is the mistake I'm doing here.

1 Answers

You cannot add the event handlers with HTML (unless you're using a framework like Angular or a compiler like Stencil).

Instead you need to use JavaScript:

document.querySelector('ion-select').addEventListener('ionBlur', () => console.log('blur'));

In a real app you'll probably want to use a more specific selector.

Related