Is there a way to display the form values on the table once it has been submitted?

I'm struggling with my form input not showing up under the table headings after submission. I've reviewed the code multiple times, but can't figure out what's causing the issue.

If you have any suggestions on how to write the code more efficiently and correctly, please share your insights.

After entering values and submitting, the table remains unchanged with no updates or modifications.

var form = document.getElementById("form");
var table = document.getElementById("table");

form.addEventListener("submit", addItem);

function addItem(e) {
    e.preventDefault();

    var customerName = document.getElementById("customerName").value;

    var itemPurchased = document.getElementById("itemPurchased").value;

    var quantity = document.getElementById("quantity").value;

    var date = document.getElementById("date").value;

    var row = table.insertRow(2);
    
    var customerCell = row.insertCell(0);
    customerCell.innerHTML = customerName;

    var ItemCell = row.insertCell(1);
    ItemCell.innerHTML = itemPurchased;

    var quantityCell = row.insertCell(2);
    quantityCell.innerHTML = quantity;

    var dateCell = row.insertCell(3);
    dateCell.innerHTML = date;

    var DeleteCell = row.insertCell(4);
    DeleteCell.innerHTML = x;
}
<div class="container">
    <div class="Form-container">
        <form id="form">
            <label for="customerName" class="input-title">CUSTOMER NAME</label><br>
            <input type="text" class="record-input" id="customerName" required><br><br><br>

            <label for="itemPurchased" class="input-title">ITEM PURCHASED</label><br>
            <input type="text" class="record-input" id="itemPurchased" required><br><br><br>

            <label for="quantity" class="input-title">QUANTITY</label><br>
            <input type="text" class="record-input" id="quantity" required><br><br><br>

            <label for="date" class="input-title">Date</label><br>
            <input type="date" class="record-input" id="DATE" required><br><br>

            <input type="submit" value="Submit" class="record-submit">

        </form>

    </div>
    <div class="record-container" id="container">
        <table id="table">
            <tr>
                <th>CUSTOMER NAME</th>
                <th>ITEM PURCHASED</th>
                <th>QUANTITY</th>
                <th>DATE</th>
            </tr>

            <tr>
                <td>EBUBE ODINAKA</td>
                <td>iPhone 11 pro max</td>
                <td>1</td>
                <td>07/01/2021</td>
                <td class="deleteTable"><button>x</button></td>
            </tr>
        </table>
    </div>
</div>

Answer №1

Issue resolved, feel free to test it out

//UPDATED JAVASCRIPT CODE
var form = document.getElementById("form");
var table = document.getElementById("table");
//add event listener to the form 
form.addEventListener("submit", addItem);

function addItem(e) {
  e.preventDefault();

  // get customer name input value
  var customerName = document.getElementById("customerName").value;

  //get item purchased input value
  var itemPurchased = document.getElementById("itemPurchased").value;

  //get quantity input value
  var quantity = document.getElementById("quantity").value;

  // get date input value
  var date = document.getElementById("DATE").value;

  //create new rows for data entry
  var row = table.insertRow(2);
  //assign values to respective cells in the row
  var customerCell = row.insertCell(0);
  customerCell.innerHTML = customerName;

  var itemCell = row.insertCell(1);
  itemCell.innerHTML = itemPurchased;

  var quantityCell = row.insertCell(2);
  quantityCell.innerHTML = quantity;

  var dateCell = row.insertCell(3);
  dateCell.innerHTML = date;

  var deleteCell = row.insertCell(4);
  deleteCell.innerHTML = "<button>x</button>";

}
<!-- Main content section -->
<div class="container">
  <!-- Form section for data input -->
  <div class="Form-container">
    <form id="form">
      <label for="customerName" class="input-title">CUSTOMER NAME</label><br>
      <input type="text" class="record-input" id="customerName" required><br><br><br>

      <label for="itemPurchased" class="input-title">ITEM PURCHASED</label><br>
      <input type="text" class="record-input" id="itemPurchased" required><br><br><br>

      <label for="quantity" class="input-title">QUANTITY</label><br>
      <input type="text" class="record-input" id="quantity" required><br><br><br>

      <label for="date" class="input-title">Date</label><br>
      <input type="date" class="record-input" id="DATE" required><br><br>

      <input type="submit" value="Submit" class="record-submit">

    </form>

  </div>
  <!-- Table section displaying records -->
  <div class="record-container" id="container">
    <table id="table">
      <tr>
        <th>CUSTOMER NAME</th>
        <th>ITEM PURCHASED</th>
        <th>QUANTITY</th>
        <th>DATE</th>
      </tr>

      <tr>
        <td>EBUBE ODINAKA</td>
        <td>iPhone 11 pro max</td>
        <td>1</td>
        <td>07/01/2021</td>
        <td class="deleteTable"><button>x</button></td>
      </tr>
    </table>


  </div>

</div>

Answer №2

<!-- This portion is written in HTML -->

    <!-- Primary section -->
         <div class="container">
            <!-- Form segment -->
            <div class="Form-container">
                <form id ="form">
                    <label for="customerName" class="input-title">CUSTOMER NAME</label><br>
                    <input type="text" class="record-input" id="customerName" required><br><br><br>

                    <label for="itemPurchased" class="input-title">ITEM PURCHASED</label><br>
                    <input type="text" class="record-input" id="itemPurchased" required><br><br><br>

                    <label for="quantity" class="input-title">QUANTITY</label><br>
                    <input type="text" class="record-input" id="quantity" required><br><br><br>

                    <label for="date" class="input-title">Date</label><br>
                    <input type="date" class="record-input" id="DATE" required><br><br>

                    <input type="submit" value="Submit" class="record-submit">

                </form>

            </div>
            <!-- Table segment -->
            <div class="record-container" id ="container">
                <table id ="table">
                    <tr>
                        <th>CUSTOMER NAME</th>
                        <th>ITEM PURCHASED</th>
                        <th>QUANTITY</th>
                        <th>DATE</th>
                    </tr>
                   
                     <tr>
                         <td>EBUBE ODINAKA</td>
                         <td>iPhone 11 pro max</td>
                         <td>1</td>
                         <td>07/01/2021</td>
                         <td class="deleteTable"><button>x</button></td>
                     </tr>
                </table>


            </div>

            </div>
//MY JAVASCRIPT CODE
var form = document.getElementById("form");
var table = document.getElementById("table");
//add event listener to form 
form.addEventListener("submit", addItem);

function addItem(e) {
  e.preventDefault();

  // get customer name input value
  var customerName = document.getElementById("customerName").value;

  //get item purchased input value
  var itemPurchased = document.getElementById("itemPurchased").value;

  //get quantity input value
  var quantity = document.getElementById("quantity").value;

  // get date input value
  var date = document.getElementById("DATE").value;

  //create delete button
  var deleteBtn = "<button>x</button>"

  var rowContent = [customerName, itemPurchased, quantity, date, deleteBtn]

  //create rows
  var row = table.insertRow(2);
  //create row cells
  rowContent.map((value, index)=>{
      var rowCell = row.insertCell(index);
      rowCell.innerHTML = value;
  });

}

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

django problem with bootstrap modal

I'm having trouble with the Bootstrap 4 modal in my Django project. When I click the button, the fade effect covers the entire screen, but the modal doesn't appear. I'm currently using Django 1.9. Does anyone have any suggestions on what mi ...

Error! React is unable to find the window object

I recently added the "react-speech" package to my application in order to incorporate text-to-speech functionality. However, upon importing the package, I encountered an error that has been challenging to resolve despite extensive research. Any assistance ...

The modals equipped with data-dismiss="modal" and data-target="#id" do not navigate to their intended destination

Attempting to focus the input field using the modal data-target function on button click. The input field is focused, but a div called <div class="modal-backdrop in"> is created and the modal does not close properly. This prevents the user from click ...

Is it possible to compile a TypeScript file with webpack independently of a Vue application?

In my Vue 3 + Typescript app, using `npm run build` compiles the app into the `dist` folder for deployment. I have a web worker typescript file that I want to compile separately so it ends up in the root of the `dist` folder as `worker.js`. Here's wha ...

Leveraging JQuery's parseJSON() function

Below is the code snippet I am working with: function responseLogin(response) { var newArray = jQuery.parseJSON(response); alert(newArray.AccountEmail); } When this code runs, it alerts the following message: {"AccountEmail":["Alphabets and numbers only ...

What is the method for accessing cookies using JSONP?

Hello, I am trying to load a page from index.html using the following code: <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.2.min.js"></script> <script> function jsonCallback(json){ console.log(json); alert(document.c ...

Bootstrap 5 and Vue 3 team up to create a stunning masonry layout

I've been struggling to find a proper solution for implementing a masonry layout in Vue 3 with Bootstrap. I tried using Bootstrap 5 cards with the Masonry CDN, but it resulted in squeezed and overlapping cards, failing to achieve the desired layout. I ...

Vue.js axios method returns undefined; v-for directive fails to iterate - Issue on Wordpress site

Using a shortcode, I have integrated Vue.js into a Wordpress page. pl2010_vue_init_directory = pl2010_vue_init_directory || (function(ctx) { new Vue( { el: '#'+ctx.el, data: { errorMsg: "", successMsg: "", show ...

Javascript - Editing specific markers with varying titles from the url

My website contains checkboxes in the html code: <input onclick="markAsChanged(this); toggleApproveDeny(this);" name="2c9923914b887d47014c9b30b1bb37a1_approveChk" type="checkbox"> <input onclick="markAsChanged(this); toggleApproveDeny(this);" na ...

The website is not displaying CSS styles as intended

I'm currently in the process of building a website with Bootstrap 5, but I've encountered an issue where the properties from index.css are not taking effect on index.html. Specifically, when I hover over a service card, the background is supposed ...

Having trouble getting the video to load on your Mozilla Firefox browser?

Unable to play video on Firefox. Error message: URL.createObjectURL(video): Error decoding media resource blob: NS_ERROR_DOM_MEDIA_FATAL_ERR (0x806e0005) Details: Decoder may not support the requested video format with YUV444 chroma subsampling. Tried usi ...

What is the method for applying background color to text within a textbox?

I am struggling to phrase this question properly and unable to find the solutions I need on Google. When searching, it only provides information on how to add an entire background color to a textbox, which is not what I am looking for. What I aim to achiev ...

The logical OR operator in JavaScript (denoted as ||)

Can anyone explain the functionality of this operator in JavaScript? I have come across this operator in two different contexts: //context 1 function(e){ e = e || window.event; //context 2 if(a || b) In C or C++, I understand that the return value of th ...

Are you familiar with manipulating the JSON data array retrieved from an Ajax response?

Is it possible to handle a response from AJAX request in PHP? I'm not very familiar with JavaScript, so I'm struggling with this one. This is what I have managed to put together: var base_url = 'http://dev.local/westview/public'; $(& ...

The functionality of the Bootstrap tabbed pane is not functioning correctly

I am currently in the process of creating a modal tabbed pane in bootstrap following the instructions provided in this guide. You can view the code and functionality on this fiddle. $(document).on("click","#tabs a",function(event) { alert("!!!"); ...

What is the reason behind the issue where max-height disrupts border-radius?

My inline svg is styled with the following CSS: .img { border-radius: 15px; overflow: hidden; max-width: 70vw; margin-top: 6vh; max-height: 50vh; z-index: -1; text-align: center; } Everything seems to work as expected, except f ...

Change all lowercase characters to uppercase in the font file

: I recently created a .ttf font file, but unfortunately only included capital characters. Is there a tool or simple method I could use to automatically create lowercase versions of each letter? For instance, when typing "hello" the font should display ...

Sending data between Node.js middleware components

if (token_count == 1) { var user_name = rows[0].user_name; next(); } else { data = { message: "Invalid Token" } res.send(data); } I must pass the parameter user_name to function next(). When next() is called, it triggers the fo ...

PHP Implementing real-time dynamic categories

I am working on a project where there are multiple items listed with unique IDs. Each item has corresponding data with the same ID. I want to use JQuery, JScript, and PHP to display options based on the ID of the selected item in real-time. Below is a snip ...

I encountered a SyntaxError indicating a missing ")" right before utilizing jQuery in my code

When trying to run the code below, I encountered an error in the console stating SyntaxError: missing ) after argument list. $(document).ready(function() { $(".blog-sidebar-form form").before("<p class="blog-sidebar-inform>Dummy Text</ ...