This is for testing purposes only.
i'm trying to store data from 3 inputs (types: text,number) and store them in localStorage. which continuously increment every submit.
i seem to be overwriting value0, value1, value2 on the second submission instead of further incrementing to value3, value4, value5.
expected result:
- first submit value0,value1,value2
- second submit value3,value4,value5
- third submit value6,value7,value8 etc.
JS:
document.getElementById('btn').addEventListener('click', createEntry());
function createEntry(){
const inputs = document.getElementsByTagName("input");
let i;
for (i = 0; i < inputs.length; i++) {
let value = inputs[i].value;
localStorage.setItem(`value${i}`, JSON.stringify(value));
}
};
gather all input by grabbing all input tags, loop through found elements. assign new statement to looped values and store them in localStorage. increment the item name to avoid overwriting the previously looped item.
HTML:
<div class="form">
<input type="text" required>
<input type="text" required>
<input type="number" min="1" max="120" required>
<button type="button" id="btn">Add</button>
</div>