Skip to content
Advertisement

Display table on button click in Javascript

I am building a sample project named: Tennis Club Management using javascript, HTML, CSS, bootstrap. In this i have a Login Page (index.html) and Manage Players Page (managePlayers.html). In managePlayers.html, I have two buttons namely Add Player and Show Players. On click events of the two buttons, I need to show the tables.

Add Players click will show the table created in HTML and Show Players click will show table created in javascript as it will dynamically display table data based on the data fetched from API.

The Problem I am facing is when I click Show Player button the table gets displayed, and after that when I click Add Player button, the table does not show up.

Below are the code files and screenshots:

index.html

JavaScript

managePlayers.html

JavaScript

Screenshots

Table show on show players click

Nothin happens when I click on Add players

No table display

Any Solution please ?

https://jsfiddle.net/mohitsharma1991/s2ncwqvL/1/

Advertisement

Answer

You need to use the tbody with id selector and hide add Player section if the show players is clicked or vise versa when the show players is clicked you need to set the add players to display:none

The reason it was not working with your existing code was that you were using innerHTML and clearing it out completely from the DOM when you click showplayers().

Once you were clicking it back again the DOM is cleared with add player section and does not exist anymore.

Live Demo:

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement