0

Using angular 11 and trying to get the value of the option element when I select an option. I've looked at probably 20 docs / so answers that say to do this:

html:

    <select class="ui-select ui-select--large" (change)="fn($event)">
      <option [value]="null" selected disabled>Select somthing</option>
      <ng-container *ngIf="values">
         <option *ngFor="let value of values [value]="value.id">{{ value.name }}</option>
      </ng-container>
    </select>

ts:

  fn(event) {
    console.log(event.target);
  }

When I select an option the select element itself gets logged, not the option element that I selected. Why?

HelloWorld
  • 2,480
  • 3
  • 28
  • 45
  • 2
    Because it's the `select` element that `change`d... – Niet the Dark Absol May 21 '21 at 06:13
  • The change event it's on select so it's normal the target is it self – Simone Rossaini May 21 '21 at 06:14
  • try `event.target.value` to get the selected value? – cloned May 21 '21 at 06:14
  • @NiettheDarkAbsol then why does this answer which was accepted with 800+ upvotes say to do this? https://stackoverflow.com/questions/33700266/how-can-i-get-new-selection-in-select-in-angular-2 (many other answers also say this) Anyway if this is wrong, then how can I access the option value? – HelloWorld May 21 '21 at 06:17
  • This isn't like the click event. As mentioned in the comments, it is the `select` element that is the target of the change event. The options aren't changing – Jon P May 21 '21 at 06:17
  • @JonP please see my comment above – HelloWorld May 21 '21 at 06:19
  • 1
    @HelloWorld, you are miss reading `$event.target.value` if that is what you are referring to in the link. That is the value of the select box, determined by the selected option. Form elements have a value attribute/property – Jon P May 21 '21 at 06:21
  • @JonP - looking at the event object you are definitely right! If you'd like please make this an answer so I can accept and others can learn. – HelloWorld May 21 '21 at 06:23

2 Answers2

3

Basically, the select element is the target of the change event. The options don't change. This is a little different from the click event.

The select element has a value attribute/property you can access with event.target.value

Jon P
  • 19,442
  • 8
  • 49
  • 72
1

you can use a more closer Angular way, see the docs

Usign a variable and ngModel two binding

//define a variable in ts
value:any
<select class="ui-select ui-select--large" [(ngModel)]="value">
   ...
</select>
{{value}} <--to check the variable

You always has in the variable "value" the value

Using two binding "split" the bannana notation to add a function:

//define a variable in ts
value:any
<select class="ui-select ui-select--large" [ngModel]="value"
   (ngModelChange)="value=$event;fn($event)">
   ...
</select>
{{value}} <--to check the variable

Using a template reference variable

//define a variable in ts
value:any
<select #myselect class="ui-select ui-select--large" 
   [value]="value"
   (change)="fn(myselect.value)">
   ...
</select>

See that, in this case "value" not is actualized -only it's used to give an initial value

Eliseo
  • 50,109
  • 4
  • 29
  • 67