Reputation: 39
It is possible on my web page when one of the buttons in a menu is clicked, a table will appear and when another button is clicked it will replace the table into another table with different data?
I have tried different possibilities, somehow it works but it doesn't replace the table.
document.getElementById("table1").style.display="none";
Upvotes: 0
Views: 1712
Reputation: 878
a sample for your question is the code below:
<!DOCTYPE html>
<html>
<head>
<title>Appear and Disappear</title>
</head>
<body>
<nav>
<button onclick="showTable1()">showTable1</button>
<button onclick="showTable2()">showTable2</button>
</nav>
<table id='table1' style="display:none">
<tr>
<th>Table Name</th>
</tr>
<tr>
<td>
It's table1
</td>
</tr>
</table>
<table id='table2' style="display:none">
<tr>
<th>Table Name</th>
</tr>
<tr>
<td>
It's table2
</td>
</tr>
</table>
<script>
function showTable1() {
document.getElementById("table1").style.display = "block";
document.getElementById("table2").style.display = "none";
} function showTable2() {
document.getElementById("table1").style.display = "none"; document.getElementById("table2").style.display = "block";
}
</script>
</body>
</html>
Upvotes: 0
Reputation: 354
document.getElementById("btn1").addEventListener("click", function(){
document.getElementById("table1").style.display = "block";
document.getElementById("table2").style.display = "none";
});
document.getElementById("btn2").addEventListener("click", function(){
document.getElementById("table2").style.display = "block";
document.getElementById("table1").style.display = "none";
});
<button id="btn1">button 1</button>
<button id="btn2">button 2</button>
<table id="table1" style="width:100%;display:none">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
</table>
<table id="table2" style="width:100%;display:none">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
Upvotes: 3