What is the best way to incorporate additional list items into a field input?

I have been working on developing a simple to-do app and I am encountering an issue. After the user clicks enter in the input box, nothing happens as expected. Despite trying various methods, the problem persists. Below is the recent code that I have been working with. Any suggestions would be greatly appreciated. Thank you.

UPDATE: The issue has finally been resolved.

$(document).ready(function() { // Input - creating a new element // $(':input').on('keypress', function(e) { if (e.which == 13 && $('#text').val().length != 0) { var input = ($this).val() $('.elements').append('<div class="text-box"></div>'); // i would like to add other elements inside a div, but i need to first get it work. // } }); })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <header> <img src="img/bg-desktop-dark.jpg" alt="background"> </header> <section> <div class="container section-content"> <div class="headline"> <h1>TODO</h1> <img src="img/icon-sun.svg" class="switcher" alt="switcher"> </div>
<div class="create">
<p class="circle"></p> <form><input type="text" placeholder="Create a new todo" id="text"></form> </div>
<div class="elements"> <div class="text-box"> <p class="circle"><img class="img-inactive" src="img/icon-check.svg"></p> <p class="text">Create a to-do app</p> </div> <div class="text-box"> <p class="circle"><img class="img-inactive" src="img/icon-check.svg"></p> <p class="text">Create a to-do app</p> </div> <div class="text-box"> <p class="circle"><img class="img-inactive" src="img/icon-check.svg"></p> <p class="text">Create a to-do app</p> </div> </div>
<div class="bottom"> <p class="items-left">5 items left</p> <div class="functions"> <p class="all">All</p> <p class="active">Active</p> <p class="completed">Completed</p> </div> <p class="clear">Clear completed</p> </div> </div> </div> </section>

Answer №1

You are on the right track, but there are a few adjustments needed to make everything work smoothly:

  1. Consider removing the <form> tag if you don't need to submit any data to a server, as it may interfere with your functionality.

  2. Make sure to use $(this) instead of ($this) within your jQuery code to reference the current element correctly.

  3. When appending elements, ensure that the content is visible by adding the input text to the body of the div rather than just creating an empty one.

$(document).ready(function() {

  // Creating a new element based on user input //

  $(':input').on('keypress', function(e) {
    if (e.which == 13 && $('#text').val().length != 0) {
      var input = $(this).val()

      $('.elements').append(`<div class="text-box">${input}</div>`);

      // Additional elements can be added inside this div once the basic functionality works //

    }
  });
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
  <img src="img/bg-desktop-dark.jpg" alt="background">
</header>

<section>
  <div class="container section-content">

    <div class="headline">
      <h1>TODO</h1>
      <img src="img/icon-sun.svg" class="switcher" alt="switcher">
    </div>

    <div class="create">

      <p class="circle"></p>
      <input type="text" placeholder="Create a new todo" id="text">
    </div>

    <div class="elements">

      <div class="text-box">
        <p class="circle"><img class="img-inactive" src="img/icon-check.svg"></p>
        <p class="text">Create a todo app</p>
      </div>

      <div class="text-box">
        <p class="circle"><img class="img-inactive" src="img/icon-check.svg"></p>
        <p class="text">Create a todo app</p>
      </div>

      <div class="text-box">
        <p class="circle"><img class="img-inactive" src="img/icon-check.svg"></p>
        <p class="text">Create a todo app</p>
      </div>

    </div>

    <div class="bottom">
      <p class="items-left">5 items left</p>
      <div class="functions">
        <p class="all">All</p>
        <p class="active">Active</p>
        <p class="completed">Completed</p>
      </div>
      <p class="clear">Clear completed</p>


    </div>

  </div>
  </div>
</section>

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

Is there a way to design a catalog page for my website that doesn't include a shopping cart or detailed product pages?

I am looking to create a catalogue feature similar to Newegg's for my website, but with a simplified approach. I have never attempted something this advanced before and I am wondering if it is possible to achieve. My plan is to use PHP and JS for the ...

I am looking to include both the type and maxLength attributes in a MUI TextField

<TextField value={ele.mobile} helperText={ferrors[id]?.mobile} name="mobile" classes={{ root: classes.textField }} InputProps={{ clas ...

Having issues with TableExport.js exporting Bootstrap HTML tables

I've been trying to use the TableExport.js plugin found at to add Bootstrap HTML table export functionality to my website. I meticulously followed all the steps to include jquery FileSaver, tableExport javascripts, and css. <!-- jQuery --> &l ...

There seems to be an issue with the accurate calculation of the screen width while utilizing the scrollbar-gutter

Please take note: The scrollbar-gutter: stable; property is not compatible with Safari. Additionally, the issue seems to be specific to Chrome and works fine in Firefox. I have observed some unusual behavior when attempting to position elements fixed to t ...

Steps to retrieve specific text or table cell data upon button click

Greetings, I am a beginner in the world of html and javascript, so please bear with me :) My challenge involves working with a table in html. Each row contains a dropdown menu (with identical options) and a button. When the button is clicked, I aim to sen ...

Develop objects with a fixed position - three.js

I am currently working on a game utilizing the three.js library and I have a specific requirement. I would like to incorporate a small "panel" on the screen that remains stationary regardless of the user's navigation through the 3D environment. Essent ...

In one application, there are two connections established with mongoose. The purpose of the second connection is to establish a dependency on the

Seeking advice: I am facing an issue where I need to establish two separate connections to the database. The first database contains login information, while the second holds all other relevant data. Can this be achieved through integration of Node.js, m ...

Prevent the routing on page reload in AngularJS

My route provider setup looks like this: app.config(function ($routeProvider, $locationProvider){ $locationProvider.hashPrefix(''); $routeProvider .when('/', { templateUrl: 'login.html', controller: 'loginCtrl&a ...

Remove nested comments using Ajax

I'm encountering a problem while attempting to delete my comments using Ajax. I believe I am on the right track but could use some guidance. Still in the process of familiarizing myself with jquery and similar technologies. While I can remove the comm ...

Saving MongoDB query results to a file using the built-in Node.js driver

I have been attempting to save the output of a MongoDB query to a file using the native Node.js driver. Below is my code (which I found on this post: Writing files in Node.js): var query = require('./queries.js'); var fs = require('fs' ...

The jQuery ajax request was unsuccessful in connecting to the remote server

I've tried researching and troubleshooting, but I still can't figure out why the Ajax code is not functioning correctly. Here is my JavaScript code: $(document).ready(function(){ $("#tform").submit(function() { var varUserName ...

adjusting the size of the sidebar at the top to ensure that it does not overlap the top menu

I have a sidebar on my page that opens vertically when I click a button. The sidebar covers the company logo and name, so I want to make it smaller. Here is the code for the sidebar: <body> <nav class="navbar navbar-dark bg-dark" ...

Initiating a click function for hyperlink navigation

Here is the HTML and JavaScript code that I am currently working with: <!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-3.3.1.min.js"></script> </head> <body> <a href="#f ...

In the world of Angular, whitespace is simply treated as non-existent when it comes

Within the controller, I have an array structured as follows: 'Pipe': '|', 'Comma': ',', 'Tab': '\t' }; When configuring ng-options in my code, it is implemented in this manne ...

Animating two elements on scroll at specific point with speed problem

Trying to create an animation with two different images once a user reaches a specific point on the page. The animation works, but there is an issue when using a trackpad - the movement appears slow compared to scrolling with a mouse. I've already att ...

What do you want to know about Angular JS $http request?

My goal is to send a request using $http with angular js in order to retrieve a json object from google maps. $http.get('http://maps.googleapis.com/maps/api/geocode/json?address=' + data[ 'street' ] + ',' + data[ 'city&a ...

Issue with useEffect causing a delay in updating the state value

I'm facing an issue with a component that displays the number of people who have liked a book. The problem is, I can't seem to consistently get the correct result in my states. Here's the code snippet: ///Fetching the book details cons ...

CSS media queries with precise inequality restrictions

Imagine the following scenario: @media only screen and (max-width: 600px) { nav { display: none; } } @media only screen and (min-width: 601px) { nav { display: block; } } This setup can lead to undefined behavior for a wid ...

I recently encountered an issue where the Google Chrome Element Inspector (Developer Tools) unexpectedly removed an HTML element from

While using WordPress, I noticed a strange issue on the admin side when utilizing Google Chrome v.32 Element Inspector (Developer Tools) - some of the elements in my HTML seem to disappear. However, when I view the page without the Developer Tools, all the ...

Hiding elements in HTML with React when data is null

Is there a way to hide elements using classes like "d-none" when the data is null in React? <span className="product__tag">{prod.tag1}</span> <span className="product__tag">{prod.tag2}</span> <span classN ...