I am in the initial stage of creating a Todolist but facing a problem creating a dynamic list using DOM methods and functions,I shall post the JS code

const BUTTON = document.querySelector('button');
const INPUTFIELD = document.querySelector('input[type = "text"]');
const UL = document.querySelector('.ul1');

INPUTFIELD = window.addEventListener('keypress',() => {
  let list = document.createElement('li');
  let value = INPUTFIELD.value;
  let dynamic = document.createTextNode(value);

Need to see HTML to effectively help here…

But what immediately glares out to me is this line:
INPUTFIELD = window.addEventListener('keypress',() => {

did you mean to do this instead??:
INPUTFIELD.addEventListener('keypress',() => {

Extra note… you have all sorts of globals going on here. This is bad practice.

Including the all caps variables. /bad

Actually I tried


but its not working so I tried the other way.I have seen many projects where devs define global variables,so is that a bad practice

Is it your goal to create a new li every time you press a key?

And yes., globals are bad practice. There’s no reason to ever use them. It’s one of the main reasons const and let were made.

Thanks for your time

TodoList initial stage


function e(){
  const BUTTON = document.querySelector('button')
const INPUTFIELD = document.querySelector('input[type="text"]')
const UL = document.querySelector('ul')
  let list = document.createElement('li')
  let value = INPUTFIELD.value
  let dynamic = document.createTextNode(value)
  if(UL.length > 3){
    console.log("size exceeded");
 // console.log(dynamic);
BUTTON = window.addEventListener('keydown',e)

One big problem is the UL elements are exceeding the space or container,any suggestions

It should have only three list items but its going on creating each time I press the key

First of all, it seems like you’re coming from another language where the const keyword creates immutable values and the convention is to capitalize the const name. That’s not how consts work in JS. Consts can have the value they reference change, they just cannot be reassigned to a new reference. We don’t capitalize them in JS land.

Now look at this line:

BUTTON = window.addEventListener('keydown',e)

This says to attach an event listener to the window, listen for when any key is pressed, and call function e, also create a global variable BUTTON to refer to this listener. What you actually want to do is find the button and attach the event listener to that. Assuming we gave our button the class submitBtn and have a function called addItem:

document.querySelector('.submitBtn').addEventListener('click', addItem);

(Give your functions more descriptive names than ‘e’ while you’re at it.)

Is this what you’re trying to achieve?

<div id="sandbox"></div>

const handleInputInput = (evt) => {
  const $lis = document.querySelectorAll('.keys_pressed li');
  if ($lis && $lis.length > 2){
  const $ul = document.querySelector('.keys_pressed');
  const $li = document.createElement('li');
  $li.innerText =;

const startSandbox = () => {
  const $input = document.createElement('input');
  $input.setAttribute('type', 'text');
  $input.addEventListener('input', handleInputInput);
  const $ul = document.createElement('ul');

  const $sandbox = document.getElementById('sandbox');

const initApp = () => {



if(lis && lis.length > 2){}

This is what I’ve been working on,didnt think of this idea