Currently, my to-do list setup requires users to click a button in order to add a new list item. I am looking to enhance the user experience by allowing them to simply hit "enter" on their keyboard to achieve the same result.
Here is the JavaScript code I utilized to dynamically create a new list item and append it to my existing list:
function newElement(){
var li = document.createElement('li');
var inputValue = document.getElementById("myInput").value;
li.appendChild(document.createTextNode(inputValue));
if (inputValue === '') {
alert("You must write something!");
} else {
document.getElementById("list").appendChild(li);
}
document.getElementById("myInput").value = "";
Below is the HTML code for the button that triggers the execution of the newElement() function mentioned above:
<input type="text" id="myInput" placeholder="Type Task Here">
<button class="submitButton" type="button" onclick="newElement()">Add To List</button>