In HTML, a table can be created using the <table>
tag and its related tags such as <tr>
for table rows, <th>
for table headers, and <td>
for table cells. To append a table for permanent storage using JavaScript, you can use the localStorage object, which allows you to store key-value pairs in the browser's local storage.
Here are the steps to append a table for permanent storage in HTML using JavaScript:
- Create an HTML table: Start by creating an HTML table using the
<table>
tag and its related tags. You can include any number of rows and columns depending on your data. For example:
php<table id="myTable">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John</td>
<td>25</td>
</tr>
<tr>
<td>Jane</td>
<td>30</td>
</tr>
</table>
- Add a button to append data: Add a button to your HTML page that will trigger the function to append data to the table. For example:
php<button onclick="appendData()">Add Row</button>
- Write a JavaScript function to append data: Write a JavaScript function that will append data to the table when the button is clicked. This function should retrieve the data to be appended from a form or input field and append it to the table using the
insertRow
andinsertCell
methods. Additionally, the function should use the localStorage object to store the data for permanent storage. Here's an example function:
javascriptfunction appendData() {
// Get the data to be appended from input fields
var name = document.getElementById("name").value;
var age = document.getElementById("age").value;
// Append the data to the table
var table = document.getElementById("myTable");
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = name;
cell2.innerHTML = age;
// Store the data in local storage
var data = { name: name, age: age };
localStorage.setItem("myData", JSON.stringify(data));
}
In this example, the function retrieves the name and age data from input fields, appends it to the table using the insertRow
and insertCell
methods, and then stores the data in the localStorage object using the setItem
method.
- Retrieve stored data: To retrieve the stored data, you can use the
getItem
method of the localStorage object. Here's an example code snippet:
javascriptvar storedData = localStorage.getItem("myData");
if (storedData != null) {
var data = JSON.parse(storedData);
var name = data.name;
var age = data.age;
// Append the retrieved data to the table
var table = document.getElementById("myTable");
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = name;
cell2.innerHTML = age;
}
In this example, the code retrieves the stored data from localStorage using the getItem
method, parses the JSON data using the parse
method, and then appends it to the table using the insertRow
and insertCell
methods.
Overall, appending a table for permanent storage in HTML using JavaScript involves creating an HTML table, adding a button to trigger a JavaScript function, writing a JavaScript function to append