Detailed explanation of dynamically generated tables using javascript

Detailed explanation of dynamically generated tables using javascript

*Create a page: two input boxes and a button

*Code and steps
/*
1. Get the input row and column values
2. Generate a table
** Loop rows
** Loop through cells in a row
3. Display on the page
- Set the table code inside the div
- Use the innerHTML property
*/

The code is as follows:

<html>
 <head>
  <title>Dynamically generate table</title>
  <style type = "text/css">
  </style>
 </head>
 <body>
   Line: <input type="text" id="h"/><br/>
   Column: <input type="text" id="l"/><br/>
   <input type="button" value="Generate" onclick="add1()"/>
   <div id="divv">
   </div>
 </body>
 <script type="text/javascript">
	function add1(){
		/*
			1. Get the input row and column values ​​2. Generate a table - Loop through rows - Loop through cells in a row 3. Display on the page - Use the innerHTML attribute to set the table code inside a div */
		var h =document.getElementById("h").value;
		//alert(h);
		var l =document.getElementById("l").value;
		var tab = "<table border='1' bordercolor='red'>";
		for(var i=0;i<h;i++){
			tab += "<tr>";
			for(var j=0;j<l;j++){
				tab +="<td>aaaa</td>"
			}
			tab +="</tr>"
		}
		tab +="</table>";
		var div1 = document.getElementById("divv");
		div1.innerHTML = tab;
	}
   </script>
</html>

Effect diagram demonstration:

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of how to implement dynamic tables in JavaScript
  • JavaScript to achieve dynamic color change of table
  • JavaScript to achieve dynamic table effect
  • JavaScript to implement a simple dynamic table
  • JavaScript to dynamically generate tables on web pages
  • Example of dynamically generating a table with JavaScript
  • Detailed explanation of how to generate dynamic tables and dynamic effects using JavaScript

<<:  Binary Type Operations in MySQL

>>:  Docker Gitlab+Jenkins+Harbor builds a persistent platform operation

Recommend

Solution to the problem of English letters not wrapping in Firefox

The layout of text has some formatting requiremen...

How to change the website accessed by http to https in nginx

Table of contents 1. Background 2. Prerequisites ...

Detailed explanation of the setting of background-image attribute in HTML

When it comes to pictures, the first thing we thi...

JavaScript countdown to close ads

Using Javascript to implement countdown to close ...

Tutorial on installing MySQL 5.6 on CentOS 6.5

1. Download the RPM package corresponding to Linu...

How to write high-quality JavaScript code

Table of contents 1. Easy to read code 1. Unified...

Details of watch monitoring properties in Vue

Table of contents 1.watch monitors changes in gen...

Summary of several principles that should be followed in HTML page output

1. DOCTYPE is indispensable. The browser determin...

MySQL operations: JSON data type operations

In the previous article, we introduced the detail...

How to modify the initial password of MySQL on MAC

Problem description: I bought a Mac and installed...

How to prevent Vue from flashing in small projects

Summary HTML: element plus v-cloak CSS: [v-cloak]...