0

I use Angular 2 in my project and I need to trigger a function either by mouse click or by tab. Currently I am able to trigger the removeFilterMsg function using mouse click, but not by tab. Here's the code:

<button _ngcontent-tvl-14="" class="c-refine-item" name="refine" role="option" aria-selected="true" title="Remove Filter">
      <span class="facet" style="color:white;font-size:20px;cursor:pointer;" aria-hidden="true" (click)="removeFilterMsg()"> </span>
</button>

For the .ts function:

removeFilterMsg() {
        const $message = document.createElement('span');
        $message.classList.add('RemoveFilterItem');
        $message.setAttribute('aria-live', 'assertive');
        $message.setAttribute('display', 'none');
        window.setTimeout(() => {
            $message.innerHTML = "Filter item is removed";
        }, 100);
        document.body.appendChild($message);
    }

Is there any way to trigger this function either by mouse click or by tab? I tried to change from (click) to (select) but it doesn't work

1 Answer 1

1

You need to use the @HostListener decorator. You can add it over your current function. If the tab key is pressed it will fire that function.

@HostListener('keydown.tab', ['$event'])
removeFilterMsg() {
    const $message = document.createElement('span');
    $message.classList.add('RemoveFilterItem');
    $message.setAttribute('aria-live', 'assertive');
    $message.setAttribute('display', 'none');
    window.setTimeout(() => {
        $message.innerHTML = "Filter item is removed";
    }, 100);
    document.body.appendChild($message);
}

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.