I have a React application in which I have a select tag with children option tags. For each option tag, I am passing some extra data that I want associated with each option and eventually retrieved to be passed into other helper functions.
const MyDropdown = () => {
const _onChange = e => {
// Here, I want to get the data attribute data-otherData and pass it into a function call.
console.log(e.target.dataset.otherData) // returns undefined
console.log(e.target.getAttribute("data-otherData") // returns null
}
return (
<select onChange={_onChange}>
<option value="USA" data-otherData="selectLocation_US">I live in USA</option>
<option value="CA" data-otherData="selectLocation_CA">I live in Canada</option>
<option value="UK" data-otherData="selectLocation_UK">I live in UK</option>
<option value="AU" data-otherData="selectLocation_AU">I live in Australia</option>
</select>
)
}
By doing various searches on the Web, I determined that I should either do event.target.dataset.otherData or event.target.getAttribute("data-otherData") in order to access my data attributes. However, none of those work.
Does anyone know what my issue is?
EDIT: I've created a fiddle to show what is happening: https://jsfiddle.net/mp8ns90o/
It was pointed out to me that the data attributes should be in kebab case and accesses to dataset should be in camelCase.
So, I've tried: I live in USA
and tried to retrieve it by using e.target.dataset.otherData and e.target.getAttribute('data-other-data') and it still doesn't work.
onChangeevent and expecting to retrieve the unique data but not getting it.