I have this simple function which toggles a hidden element in the webpage.
function showtable(id)
{
if(document.getElementById(id).style.display == 'block')
{
document.getElementById(id).style.display = 'none';
}else{
document.getElementById(id).style.display = 'block';
}
}
<input type="button" value="Toggle" onclick="showtable('id');" />
This works fine, but I want to toggle off some other (table) element (with certain ids) (except for the one which is being toggled, whether on or off) on the page every time the button is clicked.
You could use jQuery, but if you don’t want to use that; here is a pure javascript example. To see how it works, copy paste it in a text file, save it as test.htm and open it in a browser. It contains three tables, each with a button above it. When clicking a button, it’s table gets displayed and all other tables get hidden. If you need more tables, give them an id, and add their id to the array in the function:
If you want to be able to toggle that table also, it will off course also need a button:
example: