Displaying and concealing table rows based on selected items

After spending a whole day trying to get this HTML/java script to work properly, I came across some code online that I used here. My goal is to have the "Colors*" row not displayed when the page loads, but to show the color options when a shirt size is selected from the drop-down menu.

Currently, the color table with radio buttons is not displaying correctly, as it resizes and does not match the length of the row above. I am also struggling with the radio buttons allowing multiple selections instead of just one.

I have also included two images to illustrate the issue. Edit: The button problem has been resolved.

I am looking to achieve this using only JavaScript, without jQuery.

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
 <script type="text/javascript" src="jsForm.js"></script> 
<title>Javascript with Forms</title>
<style type="text/css">
table{
        width: 800px;
        padding: 10px;
    }
</style>
</head>
<body>
  <form action="#">
    <table border="1">
    <caption>T-Shirt Selection</caption>
    <tr><td style="100px;"></td><td style="150px;"></td><td     style="50px;"></td><td  style="50px;"></td><td></td></tr>
        <tr>
            <th>Description</th>
            <th>Selection</th>
            <th>Qty</th>
            <th>Total</th>
        </tr>
        <tr>
            <td>
                T_Shirt 1:  
            </td>
            <td>
                <select id="shirts" onchange="showButton(this)">
                    <option value="0" selected >Choose T-Shirt </option>
                    <option value="1" >Small $10.99</option>
                    <option value="2" >Medium $12.99</option>
                    <option value="3" >Large $14.99</option>
                    <option value="4" >X-Large $15.99</option>
                    <option value="5" >XX-Large $15.99</option>
                </select> 
            </td>
            <td>
                <input type="text" name="qty" maxlength="4" size="4" /> 
            </td>
            <td>
                <input type="text" name="qty" maxlength="10" size="10" /> 
            </td>
        </tr>


        <tr id="hidden_button" style="display:none;">
            <td colspan="1">Color.*</td>
            <td colspan="3">
            <input type="radio" name="Back" value="Black" /> Black
            <input type="radio" name="White" value="White" /> White
            <input type="radio" name="Blue" value="Blue" /> Blue
            <input type="radio" name="Red" value="Red"/> Red
            </td>   
        </tr>        </table>
    </form>
</body>

Javascript

function showButton(elem){
if(elem.value == 0){
    document.getElementById('hidden_button').style.display = "none";
}else if(elem.value > 0){
    document.getElementById('hidden_button').style.display = "block";
}

}

Here is an example of the issue: https://i.sstatic.net/RNk1a.png https://i.sstatic.net/wp7b0.png

Any assistance would be greatly appreciated.

Answer №1

Ensure that your script uses display = "table-row" rather than display = "block"

It is advisable to toggle a class instead of directly altering an element's style, as demonstrated in sample 2.

function showButton(elem) {
  if (elem.value == 0) {
    document.getElementById('hidden_button').style.display = "none";
  } else if (elem.value > 0) {
    document.getElementById('hidden_button').style.display = "table-row";
  }
}
table {
  width: 800px;
  padding: 10px;
}
<form action="#">
  <table border="1">
    <caption>T-Shirt Selection</caption>
    <tr>
      <td style="100px;"></td>
      <td style="150px;"></td>
      <td style="50px;"></td>
      <td style="50px;"></td>
      <td></td>
    </tr>
    <tr>
      <th>Description</th>
      <th>Selection</th>
      <th>Qty</th>
      <th>Total</th>
    </tr>
    <tr>
      <td>
        T_Shirt 1:
      </td>
      <td>
        <select id="shirts" onchange="showButton(this)">
          <option value="0" selected>Choose T-Shirt</option>
          <option value="1">Small $10.99</option>
          <option value="2">Medium $12.99</option>
          <option value="3">Large $14.99</option>
          <option value="4">X-Large $15.99</option>
          <option value="5">XX-Large $15.99</option>
        </select>
      </td>
      <td>
        <input type="text" name="qty" maxlength="4" size="4" />
      </td>
      <td>
        <input type="text" name="qty" maxlength="10" size="10" />
      </td>
    </tr>


    <tr id="hidden_button" style="display:none;">
      <td colspan="1">Color.*</td>
      <td colspan="3">
        <input type="radio" name="Back" value="Black" />Black
        <input type="radio" name="White" value="White" />White
        <input type="radio" name="Blue" value="Blue" />Blue
        <input type="radio" name="Red" value="Red" />Red
      </td>
    </tr>
  </table>
</form>

Sample 2

function showButton(elem) {
  if (elem.value == 0) {
    document.getElementById('hidden_button').classList.remove('show');
  } else if (elem.value > 0) {
    document.getElementById('hidden_button').classList.add('show');
  }
}
table {
  width: 800px;
  padding: 10px;
}

#hidden_button {
  display: none;
}
#hidden_button.show {
  display: table-row;
}
<form action="#">
  <table border="1">
    <caption>T-Shirt Selection</caption>
    <tr>
      <td style="100px;"></td>
      <td style="150px;"></td>
      <td style="50px;"></td>
      <td style="50px;"></td>
      <td></td>
    </tr>
    <tr>
      <th>Description</th>
      <th>Selection</th>
      <th>Qty</th>
      <th>Total</th>
    </tr>
    <tr>
      <td>
        T_Shirt 1:
      </td>
      <td>
        <select id="shirts" onchange="showButton(this)">
          <option value="0" selected>Choose T-Shirt</option>
          <option value="1">Small $10.99</option>
          <option value="2">Medium $12.99</option>
          <option value="3">Large $14.99</option>
          <option value="4">X-Large $15.99</option>
          <option value="5">XX-Large $15.99</option>
        </select>
      </td>
      <td>
        <input type="text" name="qty" maxlength="4" size="4" />
      </td>
      <td>
        <input type="text" name="qty" maxlength="10" size="10" />
      </td>
    </tr>


    <tr id="hidden_button">
      <td colspan="1">Color.*</td>
      <td colspan="3">
        <input type="radio" name="Back" value="Black" />Black
        <input type="radio" name="White" value="White" />White
        <input type="radio" name="Blue" value="Blue" />Blue
        <input type="radio" name="Red" value="Red" />Red
      </td>
    </tr>
  </table>
</form>

Answer №2

issue found in the section below

<input type="radio" name="Back" value="Black" /> Black
<input type="radio"name="White" value="White" />White
<input type="radio"name="Blue" value="Blue" />Blue
<input type="radio" name="Red" value="Red"/>Red

Due to different names given to the radio buttons, they are not grouped together. To fix this, give all of them the same name.

<input type="radio" name="color" value="Black" /> Black
<input type="radio"name="color" value="White" />White
<input type="radio"name="color" value="Blue" />Blue
<input type="radio" name="color" value="Red"/>Red

Answer №3

To start, you need to fix the first row. It contains 5 table cells but the colspan is set to only 3 columns, while all other rows (except the last hidden one) have 4 columns!!!

Having code like this in your first row is not ideal:

style="100px;"

You should include a style attribute such as width/height, etc.

There is no need to specify:

colspan="1"

As this is the default setting for table cells.

Answer №4

Here is a script that adds a class on click event for a specific element:

$('#shirts').on('click', function() { $(this).addClass('current').hidden_button(); });

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

We're facing some technical difficulties with the form for the school project. Furthermore, I need to ensure that the answers are

Upon inspection, it seems that the HTML code is fine, but there appears to be an issue with the JavaScript. I also need to store the answers in an array, which is a task for later. <form> <fieldset> <legend>Content:</lege ...

Autocomplete for Converting Postal Codes to Cities

I'm currently working on a project that involves two input fields, as shown below: <div class="form-group{{ $errors->has('plz') ? ' has-error' : '' }}"> <label for="plz" class ...

Trouble aligning a div at the center within another div using margin auto

I am facing an issue with centering a div horizontally inside another div. Here is my HTML code: .block { padding: 0px 20px; max-width: 820px; margin: 0 auto; background-color: #ff0; } .container { margin: 0 auto; display: inline-block; bac ...

Discovering identical objects by property and combining them with the help of JavaScript or UnderscoreJS

Below is an array that I have: var somevalue = [{ code: 1, name: 'a1' }, { code: 2, name: 'b1' }, { code: 1, name: 'a2' }, { code: 1, name: 'a3' }, { code: 2, name ...

Best practices for implementing JavaScript in JSP pages

After researching various sources, I have come across conflicting opinions on the topic which has left me feeling a bit confused. As someone new to web programming using java EE, I am looking to integrate javascript functions into my code. It appears that ...

Choosing a sibling element before another using CSS

Is there a way to make both of my icons display some text when hovered over? I managed to make it work, but the leftmost icon doesn't trigger the span element when hovered. The text needs to appear on the left side of the Yelp icon for design reasons. ...

How to modify the styling of an input element in React without directly manipulating the input itself

I have collected responses from a survey I created and now I want to showcase the results. The responses are rated on a scale from 1 to 5, and I would like to display them similar to the screenshot. Each number should be presented within a square, with ...

Determining Adjacency of <li> Elements Using jQuery Sortable() and Class Comparison

I have created a custom display builder specifically for outdoor power equipment dealers. This tool allows them to easily add, remove, and rearrange different product display sections according to their preferences. To achieve this functionality, I utilize ...

Generate a unique token through a personalized form

**HTML** <div ref="addCardForm" id="card-element"> <div class="card_digit_text">Credit Card Number <input type="text" id="cardDigit" name="email" placeholder="0000 0000 0000 0000"> </div> ...

How can I overlay text on top of an image?

I want to create a design where half of the text overflows and overlays the image on the right side. How can I achieve this using a Grid item? I've tried using "absolute", "relative", and "z-index" positions, but they don't seem to work in the MU ...

Angular.js is a great tool for showing PDF files on a

I am facing a challenge while trying to incorporate a PDF file into my Angular.js website. The PDF is stored as a blob in a MySQL database and I want to display it as one of the partial views that appear on the main page when a link is clicked. Unfortuna ...

What could be causing my UI Bootstrap datepicker-popup to suddenly stop functioning?

After updating to UI Bootstrap 0.11.0, I encountered an issue where my datepickers were no longer appearing correctly. To demonstrate this problem, I have created a plunker which can be viewed here. Essentially, the code snippet causing the problem is as f ...

Tips for relocating a CSS button

I have designed two buttons using css and I am looking to align them below the title "forecast customer activity". Due to extensive styling in css, the code might appear lengthy. Requesting assistance with a solution after reviewing the following code snip ...

The W3C validator does not recognize any modifications made after jQuery and JavaScript have been applied

My goal is to ensure that my website passes all errors on the W3C Validator. I have encountered a few errors related to the alt text of images. In an attempt to address this issue, I wrote and added the following code to the <head> <script type=" ...

What exactly is the functionality of the third parameter (usually next()) behind the scenes in ExpressJS once it is hidden behind the abstraction layer?

Consider this scenario: in the following two code snippets, how is the next() function used as a parameter and how does it facilitate the automatic transition to the next middleware function? What is the underlying mechanism that enables this abstraction? ...

adjust time in jQuery AJAX request when a button is clicked

I have the following code snippet that triggers when a button is clicked. When a user clicks on a button, I want to show a progress bar or waiting image in the browser for 5 seconds. How can I set a timeout and display the progress bar or waiting image wh ...

Limiting the draggable element within a compact container using jquery UI

I've been attempting to drag an <img> within a fixed-width and fixed-height container. Despite researching on Stack Overflow and finding this solution, it doesn't seem to work for my specific case. If you check out this fiddle I created, y ...

Adjust the node_modules path tailored to your project

My current project structure looks like this: |-app |-... |-libs |- package.json |- index.html I am interested in organizing my project such that the 'node_modules' folder is inside the 'libs' folder. The desired structure is as fo ...

Ways to include additional assurances depending on a specific circumstance

When my code is executed in edit mode, I have a selection of API calls that need to be made, as well as one specific API call that must be made in both create and edit modes. Here is the current code snippet: // other controller code var config = {}; ...

Arrange your grid system with Bootstrap Vue

There is a dataset retrieved from an API, and each item will be displayed within a div using a v-for loop. The desired layout for the grid of divs is as follows: [-- item 1 --][-- item-2 --] [-- item 3 --][-- item-4 --] [-- item 5 --][-- item-6 --] [-- ite ...