What is preventing the button function from being inherited by all of my newly added notes?

After the DOM is loaded, the JavaScript code below is executed:

const add_note = () => {
 // Creates a new note and its properties
 const new_note = document.createElement("div");
 const new_input = document.createElement("input");
 const new_form = document.createElement("form");
 const new_ol = document.createElement("ol");
 const new_button = document.createElement("button");

 //Fills the new note with inputs and checkboxes
 for (let i = 0; i < 5; i++) {
   let new_li = document.createElement("li");
   let new_checkbox = document.createElement("input");
   new_checkbox.setAttribute("type", "checkbox");
   let new_li_input = document.createElement("input");
   new_li_input.classList.add("li_input");
   new_ol.appendChild(new_li);
   new_li.appendChild(new_checkbox);
   new_li.appendChild(new_li_input);
 }

 //New note's settings
 new_note.setAttribute("id", "note");
 new_note.classList.add("note");
 new_note.appendChild(new_input);
 new_input.classList.add("note_input");
 new_input.setAttribute("placeholder", "Note's title");
 new_note.appendChild(new_form);
 new_form.appendChild(new_ol);
 new_ol.setAttribute("id", "ol_id");
 new_note.appendChild(new_button);
 new_button.classList.add("more_items");
 new_button.setAttribute("id", "more_items");

 //Inserts the new note and button
 const note_block = document.getElementById("notes");
 note_block.appendChild(new_note);
 const button = document.getElementById("more_items");
 button.addEventListener("click", add_more_items);
 button.innerHTML = "+";
};  

However, after creating the notes, only the first note's button seems to inherit its functions. You can see this issue in the following image: Loaded Code

I've tried multiple approaches but couldn't find a solution. Thank you in advance.

Answer №1

It is essential to remember that IDs are unique in HTML, meaning you cannot have multiple elements sharing the same ID.

If you find yourself in a situation where you need to assign the same ID to multiple elements, consider converting them into classes or finding another way to differentiate them:

new_note.setAttribute("id", "note");
...
new_ol.setAttribute("id", "ol_id");
...
new_button.setAttribute("id", "more_items");

Instead of referring directly to the button by its ID, consider using a variable:

const note_block = document.getElementById("notes");
note_block.appendChild(new_note);
new_button.addEventListener("click", add_more_items);
new_button.innerHTML = "+";

You may even want to move these last two lines up in your code before appending the note block for better organization.

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

Node.js script to convert server time to a specific local time

I have this code running on my server-side nodejs for timezone conversion. convertNowToTimezone = (localOffset) => { let d = new Date(); let millis = d.getTime() + (d.getTimezoneOffset() * 60000); //converting server local time to UTC milliseconds ...

Troubleshooting issue: The `forEach` method is not properly iterating over

I've been attempting to iterate through a response from a data file, but I keep encountering an error that I can't seem to debug or understand. I tried using the forEach and map methods within a function, but unfortunately, it didn't yield a ...

What is the best way to align a jQuery Cycle 2 Slider in the middle of the

Having some trouble centering a jQuery Cycle 2 Slider on my page. You can see the slider here. I've attempted multiple methods to center it, but none have been successful. Check out the HTML code: <div class="slider"> <div id=outside> ...

Failure to trigger success or error callbacks in Angular's $http.get request

Trying to access the nutritionix v1_1 API through a get request has been a bit tricky. Despite the function being called successfully and passing the correct data, the $http.get request seems to be causing some trouble. It simply skips over the remaining c ...

Extend the shelf life of cookies by utilizing AngularJS

I am currently implementing angularjs cookies in my project. https://docs.angularjs.org/api/ngCookies/service/$cookies Despite being aware of the security risks, I am using $cookies to store user credentials. I aim for the user to log in once and have th ...

How to properly declare an explicit injector when using the resolve parameter in $routeProvider?

$routeProvider resolve feature in AngularJS allows for injecting additional dependencies to the controller function. How can we combine this with explicit dependency injection declaration? Example: angular.module('myModule', []) .config(func ...

Is it possible to utilize ag-grid's API to filter multiple checkbox values simultaneously?

I am currently utilizing angularjs and have implemented a series of checkboxes to filter my ag-grid. So far, I have successfully utilized radio buttons and the api.setQuickFilter method for filtering based on individual values. However, I am facing an iss ...

Instructions for changing the background of a specific area to gray

Could use some assistance in changing the red area to grey, tried numerous solutions but can't figure it out. Any help would be highly appreciated! Looking to change the background to a light grey excluding the top bar and navigation. Image Current ...

Using the ol tag in Google Chrome browser

I'm dealing with an issue regarding a simple ol list in a div: <div class="one">Title<hr width="200px"> <ol> <li>One</li> <li>Two</li> <li>Three</li> </ol> </div> Here's the CSS ...

Flashing on objects, Chrome-exclusive, utilizing background-blend-mode exclusively

Our website, www.desirio.com, utilizes a background-image on the body with the following CSS property: background-blend-mode: luminosity An issue we have noticed in Chrome is that when hovering over elements, strange white lines randomly flash on the scr ...

adjust the div's position based on the window's scrolling bars

Is there a way to make my div move down when the window scrolls down and up when the window scrolls up? How can I achieve this effect? ...

Is it possible for Angular to automatically update an `ng-model` within a <tfoot> element?

I am currently working on an angular table filter. Initially, I attempted to include it in the header, but encountered issues with the icon appearing in the wrong position and the sorting changing when focusing on the text box. After moving the box to the ...

The functionality of overflow:scroll is not functioning properly on Android smartphones

Hey there! I've been trying to implement scrolling in my application using overflow:scroll, but it seems that it's not working on Android mobile phones. After some research, I discovered that Android version 3.0 and below does not support overflo ...

Unable to locate "Gruntfile.js" Node module for task execution

I am currently in the process of developing a module that enables node to execute Grunt tasks via the command line. This Node module is globally installed at : C:\Users\pcharpin\AppData\Roaming\npm\node_modules\task-app ...

What steps can I take to repair broken links in a Bootstrap template?

I am currently working with the startbootstrap-bare template from Blackrock Digital. My goal is to have different content load when a user clicks on the About link (about.html), while still keeping the navigation menu intact. However, when I change the hr ...

Utilize dropdown1 to dynamically populate dropdown 2 in AngularJS

Here is the HTML code snippet I am currently working with: <select ng-controller="category" ng-model="selectedTestAccount" ng-options="c.category for c in categories track by c.categoryID" ></select> <select ng-controller="subcategory" ng ...

What causes the table to expand with new cells once the database information is shown?

I'm confused as to why an empty cell is being added to the right even if there is data from the database to display. This particular code is meant to showcase a school schedule. Attached is a picture showing how it currently appears. Below is the PH ...

Tug and release technique

If you visit this website, you will find a detailed explanation of a drag-and-drop algorithm. Focusing on the initial code snippet provided in the article, it emphasizes the importance of using document in document.addEventListener('mousemove', ...

Issue with button vanishing upon clicking page forward in pagination for table

When I click the next button on the table pager, a button that is inline with the pager disappears. I'm not sure why this is happening. If you want to see my code, check out this JS Fiddle: $table.trigger('repaginate'); var pager = documen ...

Achieving inline navigation with hover effects

Could someone help me with creating a HTML Navbar that displays inline and has a hover effect? This is the HTML code I have been working on: <nav class="navbar navbar-light bg-faded" style="background-color: #c8c8c8"> <div class="container-flu ...