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..
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With