Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 On select change get attribute data value

My code in component.html

<select class="form-control input-sm" [(ngModel)]="o.id" formControlName="optionals" (change)="menuChange($event)">
  <option *ngFor="let menu_optional of menu_optionals" value="{{ menu_optional.id }}" [attr.data-somedata]="menu_optional.id">{{ menu_optional.name }}</option>
</select>`

And thi my component.ts

menuChange(event) {
    console.log(event.data);
}

And return is undefined

I want get value in data-somedata..

like image 340
Rifky Syaripudin Avatar asked Oct 21 '17 04:10

Rifky Syaripudin


1 Answers

As your attr.data-some-data value is the same as value you can simply write:

console.log(event.target.value);

But if you really want to get that data-attribute then use the following code:

const selectEl = event.target;

const val = selectEl.options[selectEl.selectedIndex].getAttribute('data-somedata');
// or
const val = selectEl.options[selectEl.selectedIndex].dataset.somedata;
like image 71
yurzui Avatar answered Nov 14 '22 23:11

yurzui