[SalesForce] Get field-name property on onchage event in lightning web component

I want to check on my custom onchange event which fieldName is being changed but when I try to retrieve info via event.detail.fieldName or event.detail.field-name it gives me undefined.

js:

handleInputChange(event){
    var eventName = event.detail.fieldname;
    console.log(eventName);
}

cmp:

<lightning-input-field field-name="AccountId" onchange={handleInputChange}></lightning-input-field>

How can I get field-name value via event in handleInputChange method?

Best Answer

event.detail will give you the data sent in event and event.target (or event.currentTarget) will give you the details about the elements.

Currently you are looking for attributes on source element so you should be using event.target. Below will give you fieldName:

handleInputChange(event) {
    console.log('fieldName => ', event.target.fieldName);
}

Also note

NOTE Property names in JavaScript are in camel case while HTML attribute names are in kebab case (dash-separated) to match HTML standards. In todoApp.html, the item-name attribute in markup maps to the itemName JavaScript property of c-todo-item.

Same applies even for attributes - called from same component JS.