Create a budgeting application using Vanilla JavaScript that allows users to dynamically delete rendered income or expense

I have developed a budgeting application with separate tabs for expenses and income. Each time a new expense or income is input, the details are stored in an array of objects, and then dynamically displayed as an <li> component within a <ul>. However, I am facing challenges with implementing the edit and delete functionalities. Every <li> element includes both delete and edit buttons, all sharing the same id generated by Date.now(). This unique id is based on milliseconds, ensuring no duplicates unless entries are made within the same millisecond window.

'use strict'

// JavaScript code here

@import url('https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;700&family=Raleway:wght@400;700&display=swap');
/* CSS code here */
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Budgetrr</title>
</head>
<body>
    <main class="budget-container">

        <section class="balance-container">
            <!-- HTML code here -->
        
        </section>


        <section class="budget-dashboard">
            <!-- More HTML code here -->

           
        </section>
    </main>
    <script src="JavaScript/budget.js"></script>
</body>
</html>

Despite my attempts to use .splice(), I'm struggling to successfully implement it.

Answer №1

Your input consists of objects, each with an ID property of type Date.

When you want to delete an entry, you should use the following code:

entry_list.splice(entry.id, 1)

Make sure to pass a number as argument(s) to the Javascript splice function.

To successfully delete an element, first find the ID of the element you wish to remove and then determine its index in the array. Finally, use the splice method to delete it.


Below is the process for deletion:

// Get the index of the object in the list
const index = entry_list.findIndex(x => x.id === entry.id);

// Remove one element starting from the specified index.
entry_list.splice(index, 1);

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

Transitioning from JQuery to JavaScript with Carousel Bootstrap 5

I would like to implement a bootstrap 5 carousel that displays multiple items and slides one at a time. My goal is to convert the existing Jquery code into pure Javascript so that I can eliminate the dependency on Jquery. I came across this solution which ...

Tips for extracting header ID from the HTML/DOM using react and typescript

I developed a unique app that utilizes marked.js to convert markdown files into HTML and then showcases the converted content on a webpage. In the following code snippet, I go through text nodes to extract all raw text values that are displayed and store t ...

Activate a button on-the-fly

When the page loads, I have an empty array. Since there are no values stored in this array, I disabled the button using ng-disabled="array1.length==0". Now, on the page that opens up, there are four drop downs present. My goal is to enable the button once ...

When the condition within the click function is met, concatenate the variable

I'm currently working on a function that involves adding "http://" to the variable 'bar' if it's not already included. This modified 'bar' value will then be sent via ajax to be inserted into the database and also displayed ba ...

Is there a way to make an HTML link target the same as JavaScript window.opener?

Within my project, the main page is where users log in and access the primary tables. Additionally, there are numerous supplementary pages that open in separate windows. Once the login session times out, it is essential to restrict user access to any cont ...

When the mouse hovers over a specific area, a sIFR element

Here is the html code that I am working with: <li><a href="#"><span class="font Berthold-light">1</span>Consectetur adipiscing elit risus.</a></li> I have successfully replaced the number within the span using sIFR. Ho ...

What is causing the most recent iPhones to have issues with my Javascript code?

After analyzing my webserver logs, it appears that an iOS 14 device is categorizing one of my Javascript files as "injected". This file seems to be operating in a separate environment or namespace. As a result, any AJAX attempts made by the script fail bec ...

An effective method for determining the number of <div> elements using Javascript

Hello everyone, I'm encountering an issue with my code. In my main component, I have several sub-components structured like this: <template> <div id="container"> <div v-if=condition> Component1 </div> < ...

`Is it necessary to handle textStatus when encountering an HTTP error during an AJAX request?`

When utilizing jQuery and encountering an AJAX request failure attributed to an HTTP error (e.g., 500 Internal Server Error), what exactly is the assigned value of the textStatus parameter within the error handler function? For instance, $.ajax(...).fail( ...

What setting should I adjust in order to modify the color in question?

Looking to Customize Radar Chart Highlighted Line Colors https://i.sstatic.net/PqWc4.png I am currently working on a Radar Chart and I am trying to figure out which property needs to be edited in order to change the color of the highlighted lines. I have ...

What is the best approach to reverse the selection of <li> elements by utilizing :not() and :contains

I am looking to implement a live search feature using jQuery. Below is the code snippet I have written: $("#searchInput").on("keyup", function () { var searchTerm = $("#searchInput").val(); $('li:contains("' + searchTerm + ' ...

Encountering difficulties in displaying two mapped arrays using React on the webpage

Greetings fellow beginners! I recently started using React and decided to create an app that showcases albums from the Spotify API. However, I encountered a puzzling issue - after successfully hitting the search genre button and then the search new music b ...

Assistance with themed implementation for single-page web applications in JavaScript

My goal is to incorporate theme support into my single page application. The catch is that the theme change needs to be done locally through JavaScript without making any server calls, in order to work in offline mode. Since I am using angularjs, the HTML ...

Tips for managing state updates in React Redux

Currently, I am utilizing a reducer to manage the state in Redux. The current structure of my state is as follows: { activeConversation: "Jim" conversations: (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}] user: {id: 8, username: &quo ...

Unable to adjust the color of the font

I am trying to create a toggle effect for the colors of a fontawesome icon when clicking on a text link, but it only works with a button. Here is the HTML code snippet: <div class='post'> <i class='fas fa-heart' id='h ...

Transforming HTML features into PHP scripts. (multiplying two selected values)

I am currently working on converting these JavaScript functions into PHP in order to display the correct results. I need guidance on how to use PHP to multiply the values of the NumA and NumB select options, and then show the discount in the discount input ...

Update content without reloading the page

I've implemented a function to delete an image in my action.js file: export const removeImage = (id, image_id) => async () => { try { const response = await axios.delete( `localhost:3000//api/v1/posts/${id}/delete/${image_id}`, ...

Having an issue with a 3D model in the Three.js library while working with React

View the full three.js component here VM90:1 SyntaxError: Unexpected token '<', "<!doctype "... is not valid JSON at JSON.parse (<anonymous>) at GLTFLoader.parse (GLTFLoader.js:344:17) at Object.onLoad (GLTFLoade ...

Could a commenting feature be seamlessly integrated directly into the video player, accessible with a simple click?

I'm exploring the idea of developing a video player that allows users to add comments directly from the player itself. Imagine this: the typical toolbar at the bottom includes standard features like seek bar, volume control, play/pause buttons, but wi ...

Steps for iterating through all elements within a div:

I am trying to iterate through all elements within a specific div. After attempting the following code, I realized that it only returns the main/starting div. What could be causing this issue in my code? <div id="startcontainer"> <div>anoth ...