How to create a dynamic table in JavaScript?18 Mar 2025 | 4 min read A dynamic table is one whose number of rows varies based on the input it receives during runtime. Some websites or online programs, such as business websites, require the dynamic creation of a table while adding data or information. It can be done because JavaScript is a programming language that uses dynamic type. To create a dynamic table in JavaScript, you can follow these steps: 1. Create an HTML element where you want to display the table. For example, create a div element with an id attribute to reference it in your JavaScript code: 2. In your JavaScript code, define an array of data that you want to display in the table. For example: 3. Create a function that generates the table HTML markup based on the data array. For example: 4. Call the generateTable function with the data array and append the resulting HTML markup to the table container element. For example: 5. Optionally, you can style the table using CSS to make it look more visually appealing. For example: That's it! You now have a dynamic table that can be updated with new data as needed. Example:Here's another example that generates a dynamic table using user input. Output ![]() Explanation: This code generates an input form where the user can specify the number of rows and columns they want in the table. When the user clicks the "Generate Table" button, the generateTable() function is called. This function retrieves the user input values, generates an HTML table with the specified number of rows and columns, and inserts the table into the table-container element. Note that:- This example does not use an array of data like the previous example, but instead generates the table content dynamically using loops. You can modify the loop logic to generate different table content as needed.Example: Here's another example that generates a dynamic table using data from an external API. Output
Explanation: This code uses the fetch() method to retrieve data from an external API (https://jsonplaceholder.typicode.com/users). The API returns an array of user objects, which are used to generate an HTML table with three columns (Name, Email, and Phone). The async/await syntax is used to handle the asynchronous nature of the fetch() method. Note that:- In this example, the generateTable() function is called automatically when the page loads, so the table is generated without the need for user input. You can modify the API endpoint to retrieve different data, and the table columns and content will update accordingly.Next TopicHow to remove class in JavaScript |
Lookahead patterns enable JavaScript to move forward through the string to search for specified patterns. Lookaround is a combined expression of Lookahead and Lookbehind. We can only capture a specific group using lookaheads if they arrive before another group of characters. This is useful when...
4 min read
The focus method uses the html field where the user needs to focus on a particular area. It is used on the user-interface tag or input tag. This javascript method uses for multiple input fields in the web page, and the user does not get...
3 min read
In this article, we will understand how to remove an option from the select list with the help of JavaScript. At the beginning of this article, we will learn some fundamental aspects of JavaScript, <select> tag, and remove method in JavaScript. After that, we will...
6 min read
JavaScript provides a lot of built-in string functions to perform various operations on strings. One such useful string function is endsWith(), which allows you to check if a given string ends with a specific set of characters. In this article, we will explore the endsWith()...
3 min read
Javascript offset property displays the x-coordinate or width of the specific element. The read-only MouseEvent offsetX attribute returns the x-coordinate of the mouse cursor for the target element. We can add the event function on the div tag, web page, or other elements to show...
3 min read
? JavaScript can be used to create a client-side image map. An image map is an image on the webpage that has multiple links to other pages. These links are called hotspots. An image map is used to navigate different links to other pages or on...
3 min read
The startsWith () is a method of string that determines if the string starts with the specified characters of a string. Then results either true or false, whichever is appropriate as an output. Here, in this section, we will discuss the startsWith () method with some...
3 min read
To click a link on a web page, we need to trigger a user action on the click event. But what if we want to click a link pro grammatically using JavaScript without a user event or any other phenomenon? We could accomplish this using JavaScript. There...
4 min read
A countdown timer is a wen or virtual clock that is displayed on a landing page. And it drops down from a specific date to represent the start or end of an event. A countdown timer can be used on eCommerce websites to display the start...
8 min read
JavaScript code can be run independently in a browser using the open-source, cross-platform Node.js runtime environment. The "npm" represents "Node Package Manager". The Node.js package manager is used to install various internet packages as well as dependencies into a local environment using a command-line tool. It is...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India