Skip to main content Skip to footer

Adding a Click Event to a ComboBox in Angular

Background

The ComboBox control allows users to select an item from a dropdown list. To perform a custom action when a user clicks the control, attach a native JavaScript click event listener to its hostElement.

Use the ComboBox’s initialized event to access the control and register the listener after initialization.

Steps to Complete

  1. Import the ComboBox module and define the component.
  2. Register the click listener during initialization.
  3. Create the ComboBox in the template.

Getting Started

Import the ComboBox module and define the component

import { Component, OnDestroy } from '@angular/core';
import { WjInputModule } from '@mescius/wijmo.angular2.input';
import { ComboBox } from '@mescius/wijmo.input';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [WjInputModule],
  templateUrl: './app.component.html'
})
export class AppComponent implements OnDestroy {
  countries = ['US', 'Germany', 'UK', 'Japan'];

  private comboBoxHost: HTMLElement | null = null;

  private handleClick = (): void => {
    console.log('ComboBox clicked');
  };

  initializeComboBox(comboBox: ComboBox): void {
    this.removeClickListener();

    this.comboBoxHost = comboBox.hostElement;
    this.comboBoxHost.addEventListener(
      'click',
      this.handleClick,
      true
    );
  }

  ngOnDestroy(): void {
    this.removeClickListener();
  }

  private removeClickListener(): void {
    this.comboBoxHost?.removeEventListener(
      'click',
      this.handleClick,
      true
    );

    this.comboBoxHost = null;
  }
}
  • WjInputModule provides the Angular ComboBox component.
  • The countries array supplies the dropdown items.

 

Register the click listener during initialization

this.comboBoxHost = comboBox.hostElement;
this.comboBoxHost.addEventListener(
  'click',
  this.handleClick,
  true
);
  • The hostElement property provides access to the DOM element that hosts the ComboBox. The third argument, true, registers the listener for the capture phase, allowing it to receive clicks as they travel toward elements inside the control.
  • The handleClick function logs a message to the browser console. Replace this message with the action your application needs to perform.
  • The component removes the listener in ngOnDestroy, using the same handler and capture setting supplied during registration.

 

Create the ComboBox in the template

<wj-combo-box
  #comboBox
  [itemsSource]="countries"
  (initialized)="initializeComboBox(comboBox)">
</wj-combo-box>
  • The itemsSource property binds the control to the countries array. The template reference variable, comboBox, passes the control instance to initializeComboBox when the initialized event fires.
  • This listener handles clicks within the ComboBox’s host element, including its input and dropdown button. It does not indicate that the selected item changed. Use selectedIndexChanged when your action should respond to a selection change.

 

With this setup, the ComboBox responds to clicks through a native JavaScript event listener attached to its host element, allowing you to perform custom actions when users interact with the control.

Happy coding!

Andrew Peterson

Technical Engagement Engineer