Implementing Enter key functionality to add items to a Todo list using pure DOM manipulation

var listLis=document.getElementById('list');
const addbutton=document.querySelector('.fa-plus')
const inputBar=document.querySelector('.show')


function showInput(e){
    inputBar.classList.toggle('show')
}

addbutton.addEventListener('click',showInput);
document.getElementById('myText').value='';
inputBar.addEventListener('keypress',seeToIt);

function seeToIt(e){

    var textInInput= document.getElementById('myText').value;
    var linode=document.createElement('li');
    if(e.code=='Enter'){
        linode.appendChild("Fpru");
        listLis.appendChild(linode)
        textInInput='';
    }
}

This piece of JavaScript code is for managing a To-Do List. The corresponding HTML code is as follows:

<div id="container">
    <h1>To-Do List <i class="fa fa-plus"></i></h1>
    <input type="text" id='myText' class="show" placeholder="Add New To-Do">
    <ul id='list'>

    </ul>
</div>

The issue faced by the user is related to adding new items to the To-Do List. The 'list' element is where the items should be added, and the confusion lies in triggering an event on keypress in the input field to store the li item upon pressing the enter key.

Answer №1

Is it functioning properly currently?

var listLis=document.getElementById('list');
const addbutton=document.querySelector('.fa-plus')
const inputBar=document.querySelector('.show')
const delAll = document.querySelector('.deleteAll')



function showInput(e){
    inputBar.classList.toggle('show')
}

addbutton.addEventListener('click',showInput);
document.getElementById('myText').value='';
inputBar.addEventListener('keypress',seeToIt);

delAll.addEventListener('click', ()=>{ listLis.innerHTML = ''})


function seeToIt(e){
    var textInInput= document.getElementById('myText');
    var linode = document.createElement('li');
    if(e.code=='Enter'){
        linode.innerHTML = `${textInInput.value}<button class='del' onclick="this.parentNode.remove()"><i class="fas fa-window-close"></i></i></i></button>`
        listLis.appendChild(linode)
        textInInput.value='';
    }
}
<link rel='stylesheet' href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css"
<div id="container">
    <h1>To-Do List <i class="fa fa-plus"></i></h1>
    <input type="text" id='myText' class="show"placeholder="Add New To-Do">
    <button class='deleteAll'><i class="fas fa-trash"></i></button>
    <ul id='list' >

    </ul>
    
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Numerous instances of Codemirror

I have the ability to generate and exhibit multiple dynamic codemirror instances, however, I am having trouble referencing them using the code snippet below. I suspect that the problem lies in creating a dynamic function name (not entirely sure how to ac ...

Querying MongoDB with a JavaScript file for formatting datetime values

I am utilizing mongodb3.0.5 and my database collection appears as follows: { "_id" : "xxxxxxxxxxxxxxx", "SchoolId" : 1, "ActivationTimestamp" : ISODate("2015-09-22T13:01:58.000Z"), "PersonDetails" : [ { "Name" : "John" ...

Using Sequelize and Express API for verification in the controller is an essential component of building

I'm currently working on building the API for my web app, utilizing Sequelize and Express. I have set up models with Sequelize and am in the process of developing controllers and endpoints. My main query is: Should I perform data validation checks be ...

Create objects in the gallery

I recently developed a React Material-UI component using Typescript: <Grid container direction="row" justifyContent="flex-start" alignItems="flex-start"> <Grid item xs={5}> <B ...

Troubiling Responsive Menu in React

I am currently working on developing a React Responsive Navigation with SCSS, but I am facing an issue. When I click on the hamburger button, nothing happens and the menu does not slide down in the mobile view. I tried inspecting the code in the browser to ...

What is preventing me from retrieving these JSON values?

One of the functionalities on my website involves making an AJAX call to retrieve a member's profile information for editing purposes. The code snippet responsible for this operation is shown below: function loadProfileData() { var ...

Chrome: Box-shadow not visible on images with a background present

Recently, I have noticed an issue with the box-shadow not displaying on images with a background on my website. This problem started occurring in Chrome a few months ago, despite working perfectly fine around six months ago. To pinpoint the problem, I cre ...

The state in the React components' array functions is not current

import React, { useState } from "react"; const Person = ({ id, name, age, deleteThisPerson }) => { return ( <div className="person"> <p>{name}</p> <p>{age}</p> <button onClick ...

Preview functionality is disabled in the iOS share extension

Currently, I'm developing a share extension for Safari on iOS. Our approach involves utilizing the default UI provided by iOS and extending the SLComposeServiceViewController class. In addition to this, I have incorporated a JavaScript function to ext ...

Sending form data from a third-party website to Django

Currently, I am running a Django website that holds user information. My goal is to host forms on external websites, such as a newsletter signup form. I want to be able to extract data from a queryset in the URL and send it back to my Django site. At the m ...

Group the elements of the second array based on the repeated values of the first array and combine them into a single string

I have a challenge with two arrays and a string shown below var str=offer?; var names = [channelId, channelId, offerType, offerType, Language]; var values =[647, 763, international, programming, English]; Both arrays are the same size. I want to creat ...

The request to register at http://localhost:3000/api/auth/register returned a 404 error, indicating that the

I've successfully set up a backend for user registration authentication, which works fine in Postman as I am able to add users to the MongoDB. However, when trying to implement actual authentication on localhost:3000/frontend, I encounter an error dis ...

Using Vue.js and axios to manipulate injected HTML content

I am currently working on a project using vue.js and axios to fetch the latest post content from a specific category of a WordPress site using REST API. The post content is always in the form of an ordered list (OL) which will then be displayed as a carous ...

Tips on hiding the menu toggler in PrimeNg

Struggling with PrimeNg implementation in my Angular project, I am unable to find a simple solution to hide the menu toggler. Allow me to illustrate my current setup in the first image and what I desire in the second. Let's dive in: View First Image ...

Submitting an Ajax form to dual scripts

My goal is to pass variables from a single form to two separate php scripts for processing upon submission. The current setup that I have seems to be working fine, but I'm curious if there is a way to achieve this within one jQuery script instead of d ...

Explore flat data querying using Firebase and AngularFire

I am working with data stored in firebase, utilizing a flat structure with key-value pairs to establish a many-to-many relationship between events and users (see figure 1). While it's straightforward to look up events associated with a user using pure ...

What is the best way to accomplish this using typescript/adonis?

While exploring some code examples on Bitbucket, I came across a sample that demonstrated how to paginate query results using JavaScript. However, as I attempted to apply it in my project, I encountered difficulties in declaring the types required for the ...

Leverage the input value from a text field within the same HTML document

Is it possible to utilize the input text value assigned to name='t0' within the same HTML document? Here's the code snippet you can refer to - <!DOCTYPE html> <head> <link rel="stylesheet" href="https://sta ...

Loading necessary CSS when needed in React JS

I am currently in the process of converting a bootstrap template to react framework. My question is, is there a way for me to load stylesheets on demand? For instance, if I have 2 components and I import the same stylesheet separately in both components, ...

Changing the background color of the canvas using Javascript

I want to create a rect on a canvas with a slightly transparent background, but I don't want the drawn rect to have any background. Here is an example of what I'm looking for: https://i.stack.imgur.com/axtcE.png The code I am using is as follo ...