I have some input elements and I want to create a reset button, so that I can programatically revert their values to their original state.
Here is my code:
let people = document.getElementsByClassName('person')
function reset() {
for (var key in people) {
let string = people[key].dataset.name
people[key].value = string
}
}
let app = document.getElementById('app')
let button = document.createElement('button')
button.innerHTML = "reset"
button.addEventListener('click', reset)
app.append(button)
<div id="app">
<input class="person" data-name="John" value="John">
<input class="person" data-name="Steve" value="Steve">
<input class="person" data-name="Peter" value="Peter">
</div>
This actually works as intended, but I get an error saying that the value of 'data-name' cannot be read, but this cannot be true as the function works as intended.
Does anyone know what is happening here and how I can fix this?