I am new to coding. I was trying to add input value to the givenToDos array, append this to #to-do-list div, then also append a COMPLETED button to each item. The following code is what I have tried. It is only showing the original 4 items in the array. I am stuck here, any help would be greatly appreciated.
$(() => {
const givenToDos = [
'todo1',
'todo2',
'todo3',
'todo4',
];
$('#submit').on('click', function (e) {
e.preventDefault();
const $input = $('#input-box');
const $newTodo = $input.val();
givenToDos.push($newTodo);
$input.val('');
});
for (let i = 0; i < givenToDos.length; i++) {
const $given = $(
`<div class='to-do-item' >${givenToDos[i]}<br /><button class='completed'>COMPLETED</button></div>`,
);
$('#to-do-list').append($given);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id='input-box'>
<div id='to-do-list'></div>
<button id='submit'>submit</button>