Manipulating the DOM to show various elements upon clicking on text

Currently, I am engaged in a small project that involves experimenting with DOM manipulation. The project revolves around creating a Todo list where users can generate various categories (such as work, gym, finances, etc.) and then add corresponding subtasks under each category. For instance, clicking on 'work' should display subtask1, subtask2, and so forth, while selecting 'gym' should reveal the relevant gym-related subtasks.

However, I have encountered an issue wherein I am unable to show the subtasks separately each time a main task is clicked. The majority of the functionality has been implemented using Javascript.

let index = 1;

// Remaining Javascript code omitted for brevity

The complete JavaScript code snippet is provided above.

Below is a skeleton of the HTML structure:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="\todo-list\dist\style.css" />
    <title>Document</title>
  </head>
  <body>
    <div id="content">
      // Rest of the HTML structure provided for context 
    </div>
    <script src="main.js"></script>
  </body>
</html>

Despite progress made, there seems to be an issue when clicking on a primary task, as all subtasks are displayed simultaneously. How can I modify the code to function as intended?

Please note that NPM is being utilized for this project. I have considered segregating separate arrays for individual elements and displaying them upon clicking, although I am uncertain if this aligns with proper DOM management principles.

Answer №1

Alright, I managed to come up with a potential solution using data-attributes.

Here's the complete code for anyone interested in building a Todo-List and exploring its functionality. (The CSS may need more work, but I believe the logic is solid).

Feel free to provide suggestions on improving the code or handling certain scenarios better if you have any.

// JavaScript code here
// CSS code here
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="content">
      <div class="header">
        <div class="header-text">My Todo List</div>
        <img src="/todo-list/src/assets/check-all.svg" alt="" class="header-logo" />
      </div>
      <div class="main-container" id="main-container">
        <nav class="sidebar">
          <div class="nav-text">
            <img src="/todo-list/src/assets/file-document-edit.svg" alt="" class="logo" />
            <p class="title">My Tasks</p>
            <button class="addbutton" id="addbutton">+</button>
          </div>
          <div class="added-tasks" id="added-tasks"></div>
        </nav>
      </div>
      <div class="footer">Created by Veglia Marco &#9426;</div>
    </div>
    <script src="main.js"></script>
  </body>
</html>

Appreciate any feedback. Thank you.

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

Tips for sending an array of inputs to the query selector in Node.js using Cloudant

Currently, I am attempting to retrieve documents from a cloudant database using node.js. While I have successfully managed to obtain results for a single input value, I am encountering difficulty when it comes to querying with an array of inputs. For a si ...

Transforming global CSS into CSS modules: A step-by-step guide

In my nextjs project, I am utilizing react-bootstrap and bootstrap which requires me to include the global css: // _app.js import 'bootstrap/dist/css/bootstrap.min.css'; The issue arises when loading a significant amount of unused css on every p ...

Encase every picture within a div and add a numerical label in front of it

I'm working on a function that will wrap each image in a div and add a span label for each image. Here is the code I have written so far: $('#carousel img').each(function(index) { $(this).wrap('<div class="image"></div>& ...

Following the build process with the --prod flag in Ionic 3, users may encounter a situation where

Encountering an issue only when using --prod on Android phones. Strangely, touching anywhere triggers the event that should be fired at that specific location, causing the content to suddenly appear. I came across information suggesting a conflict between ...

Ensure that the textbox remains safe from accidental keyboard inputs

How can I ensure that users can only select a date from the calendar control in my textbox, rather than typing it manually, even if I make the textbox read-only? ...

Leveraging the power of fullpage.js to activate Velocity.js/Blast.js animations

Referencing a solution shared on this forum: Velocity.js/Blast.js starting opacity at 0 I am currently working with Velocity.js and Blast.js to implement a basic word-by-word loading animation, commonly used. This setup also involves Cycle2. Additionally, ...

problem during the deployment of my node.js application on Heroku

Currently, I am in the process of deploying my node.js app to Heroku. I encountered an error when trying to execute git push heroku master: > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="127d7c3f7a77737677606152233c223c23 ...

Obtaining the source code from a different domain website with the help of jQuery

Is there a way to extract part of the source code from a YouTube page without using server-side programming? I've tried cross-domain AJAX techniques like Yahoo YQL and JsonP. While Yahoo YQL allows me to grab part of the source code, I'm facing ...

The appearance of an unforeseen * symbol caused a

Having issues with this particular line of code, import * as posenet from '@tensorflow-models/posenet' The error 'Uncaught SyntaxError: Unexpected token *' keeps popping up, I have the latest version of Chrome installed and I've ...

Converting HTML to plain text using the DOMDocument class

Is there a way to extract the text content from an HTML page source code, excluding the HTML tags? For example: <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta http-equiv="content-language" content="hu"/> <titl ...

The Ajax function effortlessly receives the returned value and smoothly transitions to the error handling stage

When trying to retrieve data from an ajax request, my function needs to receive the returned data as an array of strings. During debugging, I am able to see the response, but at the same time, the error function is triggered. This is how my code looks: ...

Using a dashed border stroke creates a unique look when combined with border-radius properties

I have been experimenting with different methods to increase the distance between border strokes on my element. So far, I have tried various approaches without success. For example, I explored this resource Unfortunately, the issue arises when I incorpor ...

Error encountered: MongoDB cast exception - nested subdocument

Check out this schema design: var messageSchema = new Schema({ receivers: [User], message: String, owner: { type: Schema.Types.ObjectId, ref: 'User' } }); var userSchema = new Schema({ name: String, photo: String }); var in ...

Updating a button via ajax to execute a php script

Hello, I'm new to using JQuery AJAX and I could use some assistance with my code. My goal is to create a toggle effect where clicking the add button changes it to a delete button, and vice versa when the delete button is clicked. However, in my curren ...

What is the best way to retrieve AJAX responses from JSON data that contains multiple sets of information

["12-Feb-2017","06-Feb-2017","5","45","40","Neha shishodia","USD","unit2","phase1","Change Request","Client Approval Awaited"]["07-Feb-2017","04-Feb-2017","6","54","48","Neha shishodia","USD","unit2","phase1","Change Request","Manager Approval Awaited"] T ...

Customize the URL path in Next.JS according to the user's location

My Next JS website is hosted on the domain abc.com. I would like the site to automatically detect a visitor's location, retrieve their country code, and then redirect them to abc.com/country_code. ...

Establishing the bottom limit for the vertical axis on a column chart with the Visualization Google API

I am currently working on implementing the Google Chart API alongside AngularJS. I attempted to adjust the min property of the vertical axis for my column chart to 0 using the following code: var dataMois = CaDuMois.query(function(){ $scope.comparat ...

Stop the stream coming from getUserMedia

After successfully channeling the stream from getUserMedia to a <video> element on the HTML page, I am able to view the video in that element. However, I have encountered an issue where if I pause the video using the controls on the video element a ...

Is it possible to use getJSON to retrieve a value from an HTML tag ID that has already been displayed on a webpage? How about using json_decode

Is there a way to retrieve the value using the HTML tag ID after an HTML page has been rendered? For example, how can I access the date value using the td_date in my JavaScript function? Below is the code snippet that populates the data on the page: listS ...

Implementing Github Oauth2 in a Rails server independent from a chrome extension

Looking to implement Github Oauth2 from my chrome extension, but rather than using chrome.identity.launchWebAuthFlow I want to handle it through my server. This way, I can avoid exposing my client ID and Client Secret in the javascript of the extension. My ...