Issue with showing the information on the table

Currently, I am working on a basic JavaScript program that requires me to collect five personal details from the user: their name, address, city, gender, and age. The goal is to display these details in a table format, but unfortunately, the program is not functioning correctly.

var btn1 = document.getElementById("btn");
btn1.addEventListener("click", details);
var row = 1;

function details() {
  var name = document.getElementById("name").value;
  var address = document.getElementById("address").value;
  var city = document.getElementById("city").value;
  var age = document.getElementById("age").value;
  var gender = document.getElementById("gender").value;

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

  var newrow = table.insertRow(row);

  var cell1 = newrow.insertCell(0);
  var cell2 = newrow.insertCell(1);
  var cell3 = newrow.insertCell(2);
  var cell4 = newrow.insertCell(3);
  var cell5 = newrow.insertCell(4);

  cell1.innerHTML = name;
  cell2.innerHTML = address;
  cell3.innerHTML = city;
  cell4.innerHTML = age;
  cell5.innerHTML = gender;

  row++;
}
<div>
  Name: <input type="text" name="n1" id="name"><br><br> Address: <input type="text" name="a" id="address"><br><br> City: <input type="text" name="c1" id="city"><br><br> Age: <input type="text" name="a1" id="age"><br><br> Gender: <input type="text" name="g1"
    id="gender"><br><br>
  <button id="btn">Submit</button><br><br>
</div>
<table id="display" border="1" cellspacing="0">
  <tr>
    <th>Name</th>
    <th>Address</th>
    <th>City</th>
    <th>Age</th>
    <th>Gender</th>
  </tr>
</table>

Answer №1

You've got a few errors in your code that need fixing. Make sure to use document.getElementById and innerHTML instead of innerHtml:

var btn1 = document.getElementById("btn");
btn1.addEventListener("click", details);
var row = 1;

function details() {
  var name = document.getElementById("name").value;
  var address = document.getElementById("address").value;
  var city = document.getElementById("city").value;
  var age = document.getElementById("age").value;
  var gender = document.getElementById("gender").value;

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

  var newrow = table.insertRow(row);

  var cell1 = newrow.insertCell(0);
  var cell2 = newrow.insertCell(1);
  var cell3 = newrow.insertCell(2);
  var cell4 = newrow.insertCell(3);
  var cell5 = newrow.insertCell(4);

  cell1.innerHTML = name;
  cell2.innerHTML = address;
  cell3.innerHTML = city;
  cell4.innerHTML = age;
  cell5.innerHTML = gender;

  row++;
}
<div>
  name: <input type="text" name="n1" id="name"><br><br> address: <input type="text" name="a" id="address"><br><br> city: <input type="text" name="c1" id="city"><br><br> age: <input type="text" name="a1" id="age"><br><br> gender: <input type="text" name="g1"
    id="gender"><br><br>
  <button id="btn">submit</button><br><br>
</div>
<table id="display" border="1" cellspacing="0">
  <tr>
    <th>Name</th>
    <th>address</th>
    <th>city</th>
    <th>age</th>
    <th>gender</th>
  </tr>
</table>

Answer №2

<div>
  information: <input type="text" name="info1" id="info"><br><br> details: <input type="text" name="d" id="details"><br><br> location: <input type="text" name="l1" id="location"><br><br> date: <input type="text" name="d1" id="date"><br><br> time: <input type="text" name="t1"
    id="time"><br><br>
  <button id="btnInfo">save</button><br><br>
</div>
<table id="displayInfo" border="1" cellspacing="0">
  <tr>
    <th>Information</th>
    <th>Details</th>
    <th>Location</th>
    <th>Date</th>
    <th>Time</th>
  </tr>
</table>
<script>
document.getElementById("btnInfo").addEventListener("click", function() {
  var info = document.getElementById("info").value;
  var details = document.getElementById("details").value;
  var location = document.getElementById("location").value;
  var date = document.getElementById("date").value;
  var time = document.getElementById("time").value;

  var tableInfo = document.getElementById("displayInfo");

  var newRowInfo = tableInfo.insertRow(rowInfo);

  var cellInfo1 = newRowInfo.insertCell(0);
  var cellInfo2 = newRowInfo.insertCell(1);
  var cellInfo3 = newRowInfo.insertCell(2);
  var cellInfo4 = newRowInfo.insertCell(3);
  var cellInfo5 = newRowInfo.insertCell(4);

  cellInfo1.innerHTML  = info;
  cellInfo2.innerHTML  = details;
  cellInfo3.innerHTML  = location;
  cellInfo4.innerHTML  = date;
  cellInfo5.innerHTML  = time;

  rowInfo++;
});

var rowInfo = 1;


</script>

Answer №3

After pasting your code into my editor for testing, I noticed a couple of things that require attention:

1- When it comes to JavaScript syntax, it's essential to remember that JavaScript is case-sensitive. This means you must follow the correct syntax when using native functions and objects in your code.

2- Make sure to adjust your approach by correcting the syntax as mentioned earlier and replacing innerHtml with innerText to populate your table correctly.

Below is the updated code reflecting all necessary changes:

var btn1 = document.getElementById("btn");
btn1.addEventListener("click", details);
var row = 1;

function details() {
  var name = document.getElementById("name").value;
  var address = document.getElementById("address").value;
  var city = document.getElementById("city").value;
  var age = document.getElementById("age").value;
  var gender = document.getElementById("gender").value;

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

  var newrow = table.insertRow(row);

  var cell1 = newrow.insertCell(0);
  var cell2 = newrow.insertCell(1);
  var cell3 = newrow.insertCell(2);
  var cell4 = newrow.insertCell(3);
  var cell5 = newrow.insertCell(4);

  cell1.innerText = name;
  cell2.innerText = address;
  cell3.innerText = city;
  cell4.innerText = age;
  cell5.innerText = gender;

  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

utilizing global definitions within Vue.js can lead to compatibility issues

Attempting to import and globally define a library has been challenging for me. Despite my efforts, the global variable is not being recognized. In main.js, import Vue from 'vue' import App from './App' import router from './rout ...

Guide to effortlessly loading files with designated decorators in a node.js application

Within the realm of Project A, I have constructed a decorator and am seeking a method to automatically load all these decorators upon initializing the application in Project B, which is the project utilizing Project A. Is there a way to accomplish this tas ...

Resetting or removing the style attribute using JavaScript in Internet Explorer 9

I have a simple webpage spinner created using the Prototype JS framework: Within the nav frame: Event.observe( 'doit', 'click', function(){ parent.window.frames.cont.spinner.removeAttribute('style'); }, ...

Error: Syntax error - V token not recognized

Although this question is quite common, I am interested in understanding the theoretical reason behind it. I am attempting to send two encrypted values to my service. Javascript var encryptedlogin = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(Ema ...

Preventing file visibility in Three.js resource directory

Once a user clicks on a specific 3D model, I retrieve it from the server and render it in the browser using three.js. However, there is an issue when the user tries to access a model that is not free - they can easily view and download the STL file by go ...

Determine the specific row number that the user clicked on within the table

I came across this code that apparently retrieves the number of the row that was clicked, but I am having trouble grasping how it works. Could someone please provide an explanation for the following code? <html> <head> <title&g ...

Guidelines for displaying a React component upon landing

I am a beginner in react app development and I am currently trying to add a component that loads when the app opens. However, I encountered an issue where the console displays the message Matched leaf route at location "/" does not have an elemen ...

`Displaying the Quantity of Items Depending on the Product Chosen in Laravel Version 11`

How can I dynamically display the quantity of items from the 'item_amount' column when a product name is selected from the 'item_name' dropdown menu in Laravel 11, based on the data stored in the database? Database id item_code item ...

The Impact of Ajax on Online Search Algorithms

There's a website that dynamically loads content at . An example page from the site can be found at: . The entire content on this page is generated using a cURL parser script. <?php $___notjson=1; ini_set('display_errors', 1); header (&a ...

Dynamically load external arrays into ECharts for setting options

Currently, I am exploring the capabilities of the ECharts library to generate charts and graphs for my website's data. Despite being new to Javascript/jQuery, I am keen on setting the xAxis data to be derived from a local page that returns a JSON Obje ...

Submitting both $_POST[] data and $_FILES[] in a single AJAX request

Struggling with uploading images along with dates on my website. I am passing the date using $_POST and the image files in $_FILES. Here is the Javascript code snippet: (function post_image_content() { var input = document.getElementById("images"), ...

How can XML data be effectively showcased on a website?

I have a service that generates XML files every 10 seconds with server information. I am seeking a solution to showcase this data on a webpage. After researching online, it appears that utilizing AJAX would be beneficial as it permits the loading of dynam ...

What is the best way to refresh or clear the DateRangePicker calendar?

Is there a way to clear/reset selected dates in the DateRangePicker calendar when using a JQuery plugin? /* Calendar init */ $('.calendar__input').daterangepicker({ alwaysShowCalendars: true, linkedCalendars: false, sho ...

What is the best way to deactivate an <a> tag in React after it has been clicked?

Is there a way to deactivate the anchor tag below in React once it has been clicked? The onClick function is not functioning on the anchor tag. <td align="left"> <input type="file" accept=".csv,.xlsx,.xls" ...

Determine the occurrences of a particular element in an array

I attempted to create a script that extracts a row from a spreadsheet filled with dates, converts it into an array, and then counts occurrences of a specific date. Although I successfully completed most of the script, I encountered difficulties when tryin ...

expanding animation featured on Wikipedia

Have you ever noticed that when you visit a Wikipedia page on Chrome and use the ctrl+scrollup or ctrl+scrolldown command, the page resizes in a smooth animation? Do you wonder how this effect is achieved? (Interestingly, in Firefox, only the links in th ...

Disregarding TypeScript import errors within a monorepo ecosystem

In my Turborepo monorepo, I have a Next.js app package that imports various components from a shared package. This shared package is not compiled; it simply contains components imported directly by apps in the monorepo. The issue arises with the shared co ...

How can we have a div stay at the top of the screen when scrolling down, but return to its original position when scrolling back up?

By utilizing this code, a div with position: absolute (top: 46px) on a page will become fixed to the top of the page (top: 0px) once the user scrolls to a certain point (the distance from the div to the top of the page). $(window).scroll(function (e) { ...

Displaying the countdown of days and hours until a specific date is just a matter of using

Currently, I am tackling a project that necessitates a specific text response related to a date object. "1 day 7 hours away" --- This format is crucial; alternatives such as "31 hours away" or "1 day away" will not suffice. -- For language switching purpo ...

Utilizing AngularJS and Bootstrap tooltips within a template

In my Rails-generated template, I am able to successfully add a Bootstrap tooltip in the application.html.erb file. However, when attempting to add a tooltip in a template partial loaded by AngularJS, it does not function as expected. Within the applicati ...