anabioze
anabioze

Reputation: 67

event.target.value - how to get the key value?

There was a need to add a drop-down list with a choice of accounts. The chosen value is processed through "event.target.value". This handler takes the value visible to the user, but I only need the 'key' value of the selected record where the stored "account.Id". I've tried get access to key, but it doesn't seem to work.

First experience with JS, so apologize in advance if the question is incorrect or elementary.

Page:

 <select class="slds-select" name = "accountSelect" onchange={changeHandler2} >
    <template for:each={allAccounts.data} for:item="account">
      <option key={account.Id} value={account.Id}>{account.Name}</option>    
    </template>
 </select> 

Handler:

changeHandler(event) {
  if (field === 'accountSelect') {
  this.accountId = event.target.options[event.target.selectedIndex].getAttribute('key');
  }
}   

Upvotes: 0

Views: 3584

Answers (1)

Did you tried to use the data to get it :

changeHandler(event) {
  if (field === 'accountSelect') {
   this.accountId = allAccounts.data.find(item => item.Id === event.target.value);
  }
}   

Upvotes: 4

Related Questions