What are the steps to create a DIV table after submitting a form?

I'm having trouble creating a header row for a DIV table using JavaScript (without jQuery). Instead of getting the expected output, I'm getting a row with the array items as headings separated by commas.

https://i.sstatic.net/8O5QR.png

var main = document.getElementById("left");

  var likertTable = document.createElement("DIV"); //main table div
  likertTable.setAttribute("class", "divTable");

  var tableHeading = document.createElement("DIV"); // table heading
  tableHeading.setAttribute("class", "divTableHeading");

  var row = document.createElement("DIV");
  row.setAttribute("class", "divTableRow");
  tableHeading.appendChild(row);
  likertTable.appendChild(tableHeading);
  main.appendChild(likertTable);

var tableHeader = ["Question", "Strongly Agree", "Agree", "Undecided", "Disagree", "Strongly Disagree"];
  for (var i = 0; i < tableHeader.length; i++) {
   var tableCell = document.createElement("DIV");
   tableCell.setAttribute("class","divTableCell");
   row.appendChild(tableCell);
   tableCell.innerHTML = tableHeader[i];
   }
.divTable{
display: table;
width: 100%;
}
.divTableRow {
display: table-row;
}
/*.divTableHeading {
background-color: #EEE;
display: table-header-group;
}*/
.divTableCell, .divTableHead {
border: 1px solid #999999;
display: table-cell;
padding: 3px 10px;
}
.divTableHeading {
background-color: #EEE;
display: table-header-group;
font-weight: bold;
}
.divTableFoot {
background-color: #EEE;
display: table-footer-group;
font-weight: bold;
}
.divTableBody {
display: table-row-group;
}
<div id="left"></div>

The variable should be var tableHeader = ranges; instead of var tableHeader = [ranges];. This mistake caused the issue in generating the correct output.

After realizing the error, everything worked perfectly when I tested the script in the snippets editor.

Grateful for the help from the community on StackOverflow.

Answer №1

var tableHeader = [ranges]; was wrongly written as var tableHeader = ranges; The mistake went unnoticed as it did not produce any error in the console even though the array was unnecessarily nested in double square brackets.

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

Although IE may have issues, @font-face is not functioning properly in Chrome and FireFox

Hello, I've encountered an issue with implementing @font-face in CSS. It seems to be working properly in Internet Explorer, but it has no effect in FireFox and Chrome. I am trying to add a custom font to my website by placing the files Snazanin.ttf an ...

Employing the identical directive within a directive [angularjs]

My requirement is to utilize the same directive within another directive, based on a conditional parameter. However, every time I attempt to do this, it appears to enter an infinite loop. It seems like the templates are being preloaded and causing a recurs ...

Incorporating pre-designed elements into the bottom section of my

I'm currently facing an issue while trying to integrate a footer content from a template into my slideout footer. The problem is that the template footer is currently spanning across the entire width of the page, and I am struggling to contain it with ...

Tips on dividing a div into two separate pages when converting to PDF

I am working on an asp.net MVC project and I need to convert a HTML div into a PDF with two separate pages. Here is an example of my code: HTML Code <div class="row" id="mycanvas"> <p>This is the first part of my content.</p> <p ...

php redirect back to the page upon form submission

I've designed an html form that allows users to input data, which will then be saved into a database using PHP: <form action="http://localhost/php/insert.php" method="post"> Barcode: <input type="text" name="barcode" /><br><b ...

`amqplib - What is the current number of active consumers on the queue?`

Seeking insight on using the amqplib module for Node JS and RabbitMQ: 1) Is there a method to determine the number of subscribers on a queue? 2) What is the process for ensuring that each queue has only one consumer key? Appreciate any guidance! ...

The span exits the external div only once the animation has concluded

I utilized jQuery to create an animation effect: http://jsfiddle.net/rxCDU/ UPDATE: Please note that this effect is optimized for Chrome browsers! The animation works correctly, however, the green SPAN element moves out of the red DIV only after the ani ...

Tips for customizing the appearance of dayMouseover in Fullcalendar

While working on my Angular application, I encountered an issue with the Fullcalendar plugin. Specifically, I want to dynamically change the cell color when hovering over a time slot in AgendaWeek view (e.g. 7.30am-8.00am). I am striving for something like ...

PHP not compatible with Fancybox iframe

I'm facing a simple problem that I can't seem to figure out. I have a button that, when clicked, opens a fancybox with an iframe displaying a page from my website. This page includes a form for sending an email. Everything works fine except that ...

Retrieve the specific file path of a dependency within the node_modules directory

Can you help me find the exact path of a dependency within a specific node_modules package? Here's an example setup: |- node_modules/ |- <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d4a4b5b7bfb5b3b1f9b594e6fae5fae4"& ...

Error in THREE.js: Unable to access property 'lib' from an undefined source (THREE.ShaderUtils.lib["normal"])

I have been working on the challenges provided in the WebGL introductory book by Oreilly. Encountered a runtime error with the following code snippet. After searching online, it seems like I am the only one facing this issue. Could you please assist me in ...

Error: Google Chrome encountered an unexpected token } that caused a syntax error

I encountered an error that reads: Uncaught SyntaxError: Unexpected token } This error only appears in Chrome, while other browsers like Mozilla and IE do not show it. Here is my script causing the issue: <script type="text/javascript" language="jav ...

How come I am unable to reinitialize my array using setState?

Within my camera module, I am aiming to store photos in a bucket every time three photos are taken. Utilizing state in react, I save the image blobs in an array. Initially, everything functions flawlessly for the first three snapshots; however, subsequentl ...

Mysterious anomaly observed: Peculiar trajectory detected in canvas image during left

I have a fascinating HTML5 Canvas that showcases the movement of two identical objects to both the right and the left. Although the right side operates flawlessly, I've noticed that the left object leaves behind an intriguing trail with a hint of gree ...

Tips for calculating the total sum of inner object property values using JavaScript, TypeScript, or Angular 5

What is the total sum of successCount values in the given array object? var successCount;//I want count of all successCount attributes from the below object var accordianData = [ { name: "Start of Day", subItemsData: [ { title: " ...

Javascript - The art of accessing an anonymous array

I'm trying to extract data from an API, but I've run into a snag. There's a list without a name attached to it, making it impossible for me to access the values inside. Here's a simplified example of what I mean: (Here is the JSON stru ...

Spring Boot fails to recognize path variable data sent from Angular

When working with Angular 7, I encountered a situation where I needed to pass a value from the service class of Angular. Here is how I achieved it: executeHelloWorldBeanServiceWithPathVariable(name){ console.log("name coming from here"+name); retu ...

`When a link is clicked, show one div overlapping another.`

Is it possible to have one div display over another when a link is clicked? Here is the HTML and CSS code: <div id="desc" class="desc">some text</div> <div id="awards" class="awards">some other text</div> <a href="#">click< ...

Accessing CSV data stored externally using JavaScript

Hi there, I am struggling to load external CSV data into a script due to what I believe is the browser's same origin policy restriction. I came across some information about using cross-document messaging as a workaround, but I have no idea how to go ...

Transferring binary fragments to Node.js for assembly into a complete file. Creating a file

Hey there, I'm in a bit of a bind. I'm trying to send file chunks using multiple XMLHttpRequest requests and then receive these parts in Node.js to reconstruct the original file from the binary data. The issue I'm facing is that the final f ...