Problem with the positioning of dynamically added row in HTML tables

I am facing an issue with my form where the details entered by the user are supposed to be saved as a row beneath the table, but they are getting saved above the heading of the table.

Here is the HTML code for the table:

//Form to be filled
<form id="expense-form" action="" method="POST">
        <h2>Form to add expenses</h2>
        <div class="form-row">
            <div class="form-group col-md-6">
                <label for="description">Budget description:</label>
                <input type="text" id="description" class="form-control" placeholder="Add description" />
            </div>
            <div class="form-group col-md-6">
                <label for="category">Category:</label>
                <select id="category" name="category">
                    <option value="Rent">Rent</option>
                    <option value="EMI">EMI</option>
                    <option value="Groceries">Groceries</option>
                    <option value="Bills">Bills</option>
                    <option value="Others">Others</option>
                </select>
            </div>
            <div class="form-group col-md-6">
                <label for="expense-amount">Amount:</label>
                <input type="number" id="expense-amount" class="form-control" placeholder="Add amount" />
            </div>
            <div class="form-group col-md-6">
                <label for="date">Expense date:</label>
                <input type="date" id="date" class="date" placeholder="Add date of the expense" />
            </div>
        </div>
        <button type="submit" id="add-btn" class="btn btn-primary">Submit</button>
    </form>


        <!--The list of expenses-->
        <table id= "expense-table" class="table table-striped mt-5">
            <thead id="expense-table-head">
              <tr>
                <th>Expense description</th>
                <th>Category</th>
                <th>Amount</th>
                <th>Expense Date</th>
                <th></th>
              </tr>
            </thead>
            <tbody id="expense-list-display"></tbody>
          </table>

The javascript function causing this issue is:

function addExpensetoList() {
var expenseDesc = document.getElementById("description").value;
var expenseCategory = document.getElementById("category").value;
var expenseAmount = document.getElementById("expense-amount").value;
var expenseDate = document.getElementById("date").value;

var table = document.getElementById("expense-table");
var row = table.insertRow(0);

var descCell = row.insertCell(0);
var categoryCell = row.insertCell(1);
var amountCell = row.insertCell(2);
var dateCell = row.insertCell(3);

descCell.innerHTML = expenseDesc;
categoryCell.innerHTML = expenseCategory;
amountCell.innerHTML = expenseAmount;
dateCell.innerHTML = expenseDate;}

After submitting the data through the form, it appears like this: https://i.sstatic.net/64vuB.png

I have been trying to figure out what I am doing wrong here. Can someone help me troubleshoot this issue?

Answer №1

The issue lies with the line of code: var row = table.insertRow(0); Instead, try using table.insertRow() without any arguments to append it as the last row.

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

Ignoring if/else statements in Jquery to handle click events

I am facing a frustrating issue with my JavaScript code. I had a simple task in mind – to have JS check if the child img of the clicked link has a specific src attribute and perform different actions based on the result. However, for some reason, the c ...

Vue components display images duplicated

My website features a news section where each card and image appear twice on the screen. Visit my CodePen profile for more information. One of the components responsible for this is called doubleUp created() { this.doubleUp() }, I'm wonde ...

Troubleshooting Vue 3 Options API custom element with non-functional Bootstrap js files

Having trouble incorporating Bootstrap 5 into a Vue 3 Options Api custom element? Check out my setup below: import { defineCustomElement } from 'vue' import 'bootstrap/dist/js/bootstrap.bundle' import App from './App.ce.vue' ...

How to retrieve client's hostname using Node and Express

How can the client's hostname be retrieved in a Node / Express server? Is there a method similar to req.connection.remoteAddress that can be used to obtain the client's hostname? ...

Ensure the table body scrolls the full height of the page while maintaining a fixed header when the table is out of view

My goal is to implement scroll bars for overflowed content within a single table on the page. Despite reading numerous posts on this topic and experimenting with various methods like setting height to 100% or using flex layout, I still can't seem to a ...

Obtain one option from the two types included in a TypeScript union type

I am working with a union type that consists of two interfaces, IUserInfosLogin and IUserInfosRegister. The TUserInfos type is defined as the union of these two interfaces. export interface IUserInfosLogin { usernameOrEmail: string; password: string; } ...

Traffic signal color transitions without the use of a button

My objective is to have the following code run automatically without the need for user input. It should also execute in a loop instead of running through the sequence only once. I attempted to modify the code, but it either failed to work or altered the sh ...

StyledTab element unable to receive To or component attributes

Is there a way to use tabs as links within a styled tab component? I've tried using the Tab component syntax with links, but it doesn't seem to work in this scenario: <Tab value="..." component={Link} to="/"> If I have ...

What is the process for altering the CSS path within an HTML helper in CakePHP 3.x?

I am trying to access the admin-css: echo $this->Html->css('adminStyle'); <link rel="stylesheet" href="/css/adminStyle.css" /> However, I would like it to be as follows: <link rel="stylesheet" href="/admin/css/adminStyle.css" /& ...

ScopeKey fails to function properly within the Nuxt.js auth module

As I delved into learning the ins and outs of Nuxt.js, I encountered a challenge while working with its Auth module. After successfully logging in, my goal was to verify the scope of the accounts. I attempted to achieve this by utilizing the "scopeKey" pr ...

The React component fails to display updates following a state change

Greetings, I am currently facing an issue with re-rendering the component. Below is the code for the initial screen where a custom component is being utilized: class Ahelle extends React.Component{ constructor(props){ super(props) this. ...

What is the best way to utilize ajax for submitting and fetching content?

It's pretty obvious that I'm a complete novice when it comes to JavaScript and jQuery, but I have a query regarding ajax requests. Here's what I'm trying to achieve but struggling with: I've defined a content.append('<di ...

Vuejs v-for nested loops

After spending countless hours researching, I am determined to solve this problem. My objective is to create a questionnaire similar to a Google Form, with question groups, questions, and answers. The structure of my data looks like this: question_group: ...

Using ThreeJS to load and display several meshes from a .json 3D file

I downloaded a .json file from an online 3D editor and now I'm facing an issue while trying to load and create 20 instances of it, similar to this example. Sadly, my code seems to be flawed as all 20 instances are behaving as if they are the same obje ...

Updating Icons on a Jquery Accordion

I need help modifying an icon to change when clicking on a specific section. Currently, I have the functionality set up to change all accordion icons when opening or closing. However, I am struggling to make it so that only the icon of the selected section ...

Upon adding data to mongodb, an error message is displayed stating "Cannot read property '_id' of undefined."

Backend Server-Side Code The following is my server-side code. Below it, you can find the client-side code along with the error that is being displayed. I am having trouble identifying what the issue might be. app.post("/service", async (res, re ...

How can I incorporate a dashed vertical line in between select bars on a bar chart using Echarts?

I am currently utilizing Echarts and have successfully developed a bar chart. My goal is to incorporate two vertical dashed lines to distinguish between Source3 and Source4, as well as another dashed line to indicate the separation of SourceSix and SourceS ...

Is your CSS failing to link or display properly?

I'm encountering a frustrating issue with my code that I suspect has a simple fix, but I can't seem to figure it out. It seems like my CSS isn't linking to my HTML file properly. I've searched through various resources for a solution, b ...

The jQuery AJAX doesn't specify the complete server path in the URL

Hey friends, I could really use some help with this situation. It's driving me mad because I've successfully done it before, but now it just won't work. Here's the issue - I have an HTML button that triggers a JavaScript function when ...

Are iFrames being utilized for third-party applications?

I am looking to create a web application that can be extended by other developers with their own applications. Would using iFrames, similar to how Facebook does it, be the best approach? Is this considered a good practice in web development? Are there a ...