What is the process for showing a table based on a selected value?

I could use some help - I am working on a <select> element with 4 different options in the form of <option>. What I want to achieve is to have tables that are initially not visible, and then when an option is clicked, the corresponding table will be displayed. How can I accomplish this?

<select name="exercise">
  <option value="16">Exercise 2016</option>// Display table if this option is clicked
  <option value="17">Exercise 2017</option>// Show different table on click
  <option value="18">Exercise 2018</option>// ***
  <option value="19">Exercise 2019</option>// ***
</select>

Answer №1

Did you consider using JQuery in your project? If not, you can implement it like this: HTML: HTML CONTENT CSS:

.table-list{
    display:none;
}

JS:

var select = document.getElementById("toggle");
toggle.addEventListener('change',function(){
    var tbs = document.getElementsByClassName('table-list');
for(var i=0;i<4;i++){
    tbs[i].style.display = 'none';
}
var tbName = select.value;
document.getElementById('tbName').style.display = 'block';
});

You can see the live demo here

Answer №2

If you want to change the visibility of a table based on the selected option, you can retrieve the index of the selected option and incorporate it into your custom function to make the table visible.

For example: Let's say you have four tables with similar names except for their endings. You can include the index of the selected option in the table name to determine which one should be made visible. By utilizing the namespace of the table along with the selected option's index, you can easily locate the specific table and adjust its visibility accordingly.

I trust this explanation will be beneficial to you :)

Answer №3

If you want to dynamically display different tables based on the value of a select box, it's actually quite simple. Here is an example:

Use the following CSS:

.tablecontainer li { display: none; }
.tablecontainer li.show { display: block; }

Here is the HTML structure:

<select name="selectbox">
  <option value="" checked>Select</option> 
  <option value="firsttable">Table 1</option> 
  <option value="sectable">Table 2</option>  
</select>

<ul class="tablecontainer">
 <li id="firsttable">
  <table>
   <tr>
    <td>First table</td>
   </tr>
  </table>
 </li>
<li id="firsttable">
  <table>
   <tr>
    <td>First table</td>
   </tr>
  </table>
 </li>
</ul>

And finally, use this JQuery script:

$('select').on("change", function(event) {
 event.preventDefault();
 $(".tablecontainer")
  .find("#" + $(this).val())
  .addClass("show")
  .siblings()
  .removeClass("show");
});

I hope this explanation and example will be helpful for you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The ion-item is refusing to be centered on the page

I'm having trouble centering an ion-item and it seems slightly off-center. The standard methods like text-align: center and adjusting padding in ion-content don't seem to be working for me. Can someone please assist? https://i.stack.imgur.com/QZ ...

Unknown provider in Angular when using factory inside anonymous function wrapper

I encountered an issue with an unknown provider error when using a factory and declaring it with an anonymous function: (function () { 'use strict'; angular.module('app').factory('errorCodeFactory', errorCodeFactory) ...

Tips for maintaining the integrity of blank space within a text node?

When intercepting a paste event and cleaning HTML off of the content using textNodes, I am faced with an issue where all white space is reduced to a single space and new lines are disregarded. For example, pasting: "hello world !" ends up being "h ...

Inconsistency found in Ajax DataTable data updates

Incorporating a DataTable ajax feature, I pass values to the controller. Here is a simplified version of the code: $(function() { $("#tableDiv").hide(); $("#submitDateFilters").on("click", function() { displayData(); $("#tableDiv").show(); ...

Activation of states in response to item clicks

Recently, I started using the US-Map plugin created by newsignature (). I have put together a chart that highlights various state laws for comparison on a per-state basis. Currently, the setup allows me to compare 3 states at a time. Users can easily clos ...

"Enjoying a table header that scrolls freely with autoscroll feature

Resolved - http://jsfiddle.net/CrSpu/11704/ I'm facing an issue with a table that has autoscroll functionality. I am looking to freeze the header of the table when automatic scrolling occurs, or you can test it out using my code pen. I'm uncer ...

Tips for adjusting font size automatically to fit within its parent container every time

On my blog, the post titles are displayed in a div with a video playing in the background. The length of the title varies, ranging from 2 words to over 20 words. When the title is too long, it may overflow from its parent container where the video is playi ...

How can I include line breaks using HTML `<br>` tags in a textarea field that is filled with data from a MySQL

Within a modal, I am showcasing a log inside a read-only <textarea> field that contains data fetched from my MySQL database. Below this, there is a writable <textarea> field where users can input updates to the log, which are then added to the ...

Flexbox allows you to easily manage the layout of your website

I am currently working on a CSS project and have encountered an issue. Whenever I apply the "display: flex" property to the .student element, the border around it mysteriously doubles. The reason for wanting to use the flex property is to align the text ve ...

What is the best way to remove bootstrap when transitioning to a new microfrontend?

Our team is facing an issue with styling when using a combination of React, Bootstrap, and Tailwind. The problem arises when linking our micro frontend to modules that use Tailwind, as the Bootstrap styles persist despite the change. Since both Tailwind an ...

Using jQuery to target nested HTML elements is a great way to efficiently manipulate

Within the code below, I have a complex HTML structure that is simplified: <div id="firstdiv" class="container"> <ul> <li id="4"> <a title="ID:4">Tree</a> <ul> <li id="005"> ...

I developed a digital Magic 8 Ball program, but unfortunately, it's only providing me with one response

I'm currently working on a Discord Bot for my friends and myself. One of the scripts I've created is an 8Ball script, but it's only giving me one answer. Here's the code snippet for my variable: var rand = ['Yes', 'No&apo ...

"Need help solving the issue of generating a random number for a Firebase DB column after adding a new user

Whenever I add a new user using JavaScript, it generates a random number below the Admins column like this. However, I want to adjust this so that it appears as shown in these tables, displaying the username value. If anyone can help me modify the code acc ...

Tips on separating/callback functions based on earlier variables

Breaking Down Callback Functions Based on Previous Variables I am trying to figure out how to efficiently break down callback functions that depend on variables defined earlier in the code. Currently, my code resembles a "callback hell" due to my lack of ...

Error in GLSL file: "Module parsing error: Unexpected symbol"

I'm currently working on creating a custom image transition using a shader, and I require a fragment.glsl file for this purpose. However, when I try to import this file into my .js file, I encounter the following error: Compiled with problems: ERROR ...

Feeling anxious about the abundance of fields within the table

Currently in the process of planning my upcoming project, a text-based MMORPG game. I'm tackling the design of certain aspects of the database and have encountered a new challenge. One feature of the game involves allowing players to purchase cars and ...

The array.slice() method fails to work properly when I try to start it from any index other than 0

I am currently working with an array called $scope.results which contains 8 objects. I have also created a custom simple pagination and a function called selectAll() that I'm trying to get to work together. Please refrain from suggesting the use of b ...

Toggle the hamburger menu using JavaScript

How can I close my hamburger menu when clicking a link for one page navigation? The menu is functioning properly, but I need a way to close it. Unfortunately, I have limited knowledge of JS. I only have the HTML and CSS for this: HTML in index.html file ...

Issue with horizontal scrolling functionality in DataTables

I have a table with over 10 columns, and I am using datatables to display the data. I have enabled horizontal scrolling due to the large number of columns, but the scroll is not appearing. Can someone please assist me with this issue? Here is a screenshot ...

Divide Angular ngFor into separate divs

Here is an example of my current array: [a, b, c, d, e, f, g, h, i] I am aiming to iterate through it using ngFor and split it into groups of 3 elements. The desired output should look like this: <div class="wrapper"> <div class="main"> ...