4

I have div with <ul> and <li> that show after ngIf performed as below:

<input type="text" />
<button (click)="ShowDropDown=true;">Show</button>
<div *ngIf="ShowDropDown">
  <ul>
    <li (mousedown)="...">...</li>
    <li (mousedown)="...">...</li>
    <li (mousedown)="...">...</li>
  </ul>
</div>

I need to set focus on the very first element of <li> after clicking on the Show button.

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
Janith Widarshana
  • 3,213
  • 9
  • 51
  • 73

3 Answers3

10
<input type="text" #myInput />
<button (click)="ShowDropDown=true;">Show</button>
<div *ngIf="ShowDropDown">
  {{ myInput.focus() }}
  <ul>
    <li (mousedown)="...">...</li>
    <li (mousedown)="...">...</li>
    <li (mousedown)="...">...</li>
  </ul>
</div>

put attention on {{ myInput.focus() }} inside ngIf and #myInput inside the input

Sergey Gurin
  • 1,537
  • 15
  • 14
  • 1
    This is the only solution that works with ngIf on the input/form-field (Angular 9) ! Thanks ! – Elvynia May 04 '20 at 12:30
5

For setting focus on element like div, li etc., you need to set a tabindex attribute for them. Here is how you can set the focus on button click:

Change your component html to this:

<button (click)="showDropDown()">Show</button>
<div *ngIf="toggleDropDown">
  <ul>
    <li #firstLi tabindex="0">...</li>
    <li >...</li>
    <li >...</li>  
  </ul>
</div>

... and in your component class to:

toggleDropDown: boolean = false;
  @ViewChild('firstLi') firstLi:ElementRef;
  public showDropDown(){
      this.toggleDropDown=true
      setTimeout(()=>{
        this.firstLi.nativeElement.focus();
      },10);
  }

Here is a working plunker: DEMO

FAISAL
  • 33,618
  • 10
  • 97
  • 105
3

I have updated my solution to use getter/setter. I have add tabindex as mentioned by @Faisal. In template:

<button (click)="showDropDown=true;">Show</button>
<div #container>
<div *ngIf="showDropDown">
  <ul>
    <li tabindex="0" (mousedown)="select">1</li>
    <li (mousedown)="select">2</li>
    <li (mousedown)="select">3</li>
  </ul>
</div>
</div>

And in component:

  private _showDropDown = false;

  get showDropDown () {
    return this._showDropDown;
  }

  set showDropDown (val) {
    this._showDropDown = val;
    this.focusOnFirstLi();
  }

  @ViewChild('container') myDiv;

  focusOnFirstLi () {
     var that = this;
     setTimeout(function () {
       that.myDiv.nativeElement.getElementsByTagName("li")[0].focus();
     }, 10);
  }

https://plnkr.co/edit/Dc3Eh35QX9kiHg3YsSBR?p=info

Badis Merabet
  • 13,970
  • 9
  • 40
  • 55
  • Hi, I have similar issue, want to display focus on the first 'li' part. The above solution is not working for me. I have ng-if and it is on ng-template. Can you help me with the same? @BadisMerabet – TheDoozyLulu Dec 23 '19 at 12:15