Creating visible and invisible containers using CSS

I am facing an issue with the type switcher and containers marked by red rectangles. Each container has its own ID, and when one option is selected in the type switcher, all other containers should be invisible. For example, if the "Furniture" option is chosen, only the container with the ID "Furniture" should be visible while the rest remain hidden. I struggle with jQuery and would appreciate any help.

https://i.sstatic.net/NoY1G.png

<form id="product_form">
  <label id="caption" style="line-height: 180%;">SKU <input type="text" id="text-field" name="sku" style="margin-left: 37px;"/></label><br />
  <label id="caption" style="line-height: 180%;">Name <input type="text" id="text-field" name="name" style="margin-left: 21px;"/></label><br />
  <label id="caption" style="line-height: 180%;">Price($) <input type="text" id="text-field" name="price" style="margin-left: 2px;"/></label><br />
  <br />
  <label id="caption">Type Switcher<select id="productType" name="type" style="margin-left: 5px;">
                    <option value="D">DVD</option>
                    <option value="B">Book</option>
                    <option value="F">Furniture</option>
                </select></label>
  <br /><br /><br />
  <div id="DVD">
    <p style="text-align: left; line-height:180%;" id="caption">Please provide size in MB</p>
    <label id="caption" style="line-height: 180%;">Size (MB) <input type="number" name="size" style="margin-left: 2px" /></label>
  </div>
  <div id="Book">
    <p style="text-align: left; line-height:180%;" id="caption">Please provide weight in KG</p>
    <label id="caption" style="line-height: 180%;">Weight (KG) <input type="number" name="weight" style="margin-left: 2px" /></label>
  </div>
  <div id="Furniture">
    <p style="text-align: left; line-height:180%;" id="caption">Please provide dimensions in HxWxL format</p>
    <label id="caption" style="line-height: 180%;">Height (CM) <input type="number" name="height" style="margin-left: 5px" /></label><br />
    <label id="caption" style="line-height: 180%;">Width (CM) <input type="number" name="width" style="margin-left: 11px" /></label><br />
    <label id="caption" style="line-height: 180%;">Length (CM) <input type="number" name="Length" style="margin-left: 2px" /></label><br />
  </div>
</form>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Answer №1

It's important to familiarize yourself with CSS...

const
  productForm = document.querySelector('#product_form')
  sBlocks     = document.querySelectorAll('#DVD, #Book, #Furniture')
  ;
productForm.type.onchange = setTypeSwitcher;

// on init page
setTypeSwitcher()

function setTypeSwitcher ()
  {
  sBlocks.forEach( blk => 
    blk.classList.toggle('noDisplay', blk.id[0] != productForm['type'].value)
    )
  }
body {
  font-family : sans-serif;
  font-size   : 14px;
  }
label {
  display : block;
  margin  : 0;
  width   : 20em;
  height  : 2em;
  padding : .2em 0 0 0;
  font-weight : bold;
  line-height : 2em;
  overflow    : hidden;
  }
label input {
  float : right;
  width : 14em;
  }
label:after {
  content: ' .  .  .  .  .  .';
  font-weight : normal;
  }
label select,
label input[type=number] {
  width      : 12em;
  text-align : center;
  float      : right;
  }
#product_form p {
  margin-bottom : .2em;
  }
.noDisplay {
  display : none;
<form id="product_form">
  <label> SKU      <input type="text" name="sku"   /> </label>
  <label> Name     <input type="text" name="name"  /> </label>
  <label> Price($) <input type="text" name="price" /> </label>
  
  <label> Type Switcher
    <select name="type">
        <option value="D">DVD</option>
        <option value="B">Book</option>
        <option value="F">Furniture</option>
    </select>
  </label>
  
  <div id="DVD">
    <p> Please provide size in MB </p>
    <label> Size (MB) <input type="number" name="size" /></label>
  </div>
  <div id="Book">
    <p> Please provide weight in KG </p>
    <label> Weight (KG) <input type="number" name="weight"  /></label>
  </div>
  <div id="Furniture">
    <p> Please provide dimensions in HxWxL format </p>
    <label> Height (CM) <input type="number" name="height" /></label>
    <label> Width (CM)  <input type="number" name="width"  /></label>
    <label> Length (CM) <input type="number" name="Length" /></label>
  </div>
</form>

Answer №2

$('#productType2').on('change', (e) => {
  $('.js-modifyByType').hide();
  $(`.js-modify${e.currentTarget.value}`).show();
});
$('#productType2').trigger('change');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form id="product_form">
  <label id="caption" style="line-height: 180%;">SKU <input type="text" id="text-field" name="sku" style="margin-left: 37px;"/></label><br />
  <label id="caption" style="line-height: 180%;">Name <input type="text" id="text-field" name="name" style="margin-left: 21px;"/></label><br />
  <label id="caption" style="line-height: 180%;">Price($) <input type="text" id="text-field" name="price" style="margin-left: 2px;"/></label><br />
  <br />
  <label id="caption">Product Type Selector<select id="productType2" name="type" style="margin-left: 5px;">
                    <option value="D">DVD</option>
                    <option value="B">Book</option>
                    <option value="F">Furniture</option>
                </select></label>
  <br /><br /><br />
  <div id="DVD" class="js-modifyByType js-modifyD">
    <p style="text-align: left; line-height:180%;" id="caption">Please provide size in MB</p>
    <label id="caption" style="line-height: 180%;">Size (MB) <input type="number" name="size" style="margin-left: 2px" /></label>
  </div>
  <div id="Book" class="js-modifyByType  js-modifyB">
    <p style="text-align: left; line-height:180%;" id="caption">Please provide weight in KG</p>
    <label id="caption" style="line-height: 180%;">Weight (KG) <input type="number" name="weight" style="margin-left: 2px" /></label>
  </div>
  <div id="Furniture" class="js-modifyByType  js-modifyF">
    <p style="text-align: left; line-height:180%;" id="caption">Please provide dimensions in HxWxL format</p>
    <label id="caption" style="line-height: 180%;">Height (CM) <input type="number" name="height" style="margin-left: 5px" /></label><br />
    <label id="caption" style="line-height: 180%;">Width (CM) <input type="number" name="width" style="margin-left: 11px" /></label><br />
    <label id="caption" style="line-height: 180%;">Length (CM) <input type="number" name="Length" style="margin-left: 2px" /></label><br />
  </div>
</form>

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

Which is the better choice for accessing and manipulating JSON files – using Ajax or the Node fs module?

When storing quiz questions using JSON data, should I use vanilla AJAX or Node.js file system to read the file? I am planning to develop a website where users can create quizzes and save them as JSON. I intend to utilize the Node.js fs module to save the ...

When working with String.fromCharCode in jQuery, it often returns undefined

I am trying to use jQuery to convert Cyrillic input to Latin characters in my code. I am attempting to find the position of Cyrillic characters in an array and replace them with their corresponding Latin characters, but I keep getting an 'undefined&ap ...

Tips for displaying multiple date choices with Bootstrap datepicker

I am currently using a bootstrap datepicker with an inline calendar. I want to enable users to select multiple days by clicking on them, instead of removing the selection each time a new day is clicked. I have attempted to add an active class when a user c ...

Transform a list that can be sorted into a query, then fetch and showcase it once more

My example demonstrates creating a string using the JQuery UI sortable event. I aim to collect multiple strings like this: $string: item[]=4&item[]=1&item[]=2 I want to select one of these strings to display on a page and plan to store them in ...

Tips for reducing the amount of white space on a card featuring a pie chart in a mobile layout

Is there a way to adjust the size of the pie chart or reduce the white space at the bottom? In the mobile view after inspecting with Chrome dev tools, it seems like the chart is not displaying as desired. Here is the screenshot <div ...

Having trouble accessing the dashboard after uploading a document to Firestore

I am currently working on a project where I need to upload an audio file to Firebase storage and also add document data related to the audio file in Firestore database. The process involves recording the audio, uploading it to Firebase storage, submitting ...

ISO format for the number of weeks in a month, considering weeks that cross over Thursday

I am looking for a precise number of weeks per month in accordance with the ISO standard. Instead of using JavaScript's Date, I am utilizing momentJS. According to the ISO date format, a month's week count is determined based on whether it cont ...

Ensure that the view is only updated once the JSON has been completely received from the AJAX call in AngularJS

Just starting out with angularJS and still in the learning phase. I'm currently working on creating a navbar for the header that will fetch data from an ajax call. The issue I'm facing is that it displays {{obj.pageData}} until the data is fully ...

Changing a DOM structure into pure HTML using JavaScript

Looking to convert some HTML into a PDF file using a service that requires form-data as the payload. Is there a way to extract HTML from the DOM and save it as a file for use in the payload? <p>some other HTML</p> <div id="content"> ...

AJAX functionality seems to be malfunctioning on mobile devices, although it is working properly on the Chrome developer tool

Hey there! I'm currently working on a client's website and encountering an issue with my AJAX call on a specific mobile page of the site. You can check out the page in question here. Interestingly, it seems like the header and footer are only mal ...

Tips for displaying animations only the first time using HTML and CSS:

Upon the initial visit to my website, a captivating animation introduces itself with the words "Hello. I'm Bob" as it gracefully fades into view. Navigating through the menu bar allows users to explore different sections of the site. However, there ...

Incorporate various variables within a JavaScript loop

I have a range of input classes from .fat-calc1 to .fat-calc24, as well as a designated result area with the class fat-result. My goal is to create a loop that accumulates all inputs (.fat-calc1 + .fat-calc2 + ... + .fat-calc24) and presents the sum in the ...

Creating specific data types in Vue.js

Is it possible to create custom prop types with extended validation for Vue.js props? In the following example, we have a prop called background that is currently of type Object. I would like to change this to a custom prop type called Image. The Image pr ...

Issue with Media Queries for adjusting width not functioning correctly

I'm currently working on a JavaScript calculator project and I'm having trouble making it responsive. Specifically, when the screen size decreases, the buttons in the calculator are not displaying properly, especially the operator buttons. I woul ...

Create a dynamic fbx model complete with textures and animations using three.js

I have been working on showcasing "animated 3D Models" on a webpage. These models come in the form of .obj, .mtl & .fbx files, some with textures and some without. I've managed to display .obj 3D Models on the webpage successfully (with texture and mt ...

Issue with Django template rendering preventing jQuery events from loading

I'm currently working on implementing a URL preview feature similar to Facebook, utilizing this jQuery plugin. When I input a URL in a standalone template, the preview is generated successfully. However, when attempting to render the template as a str ...

Reading uploaded .PDF or .DOC files as images without the need for conversion

Lately, I've been brainstorming a new feature where users can upload files such as .pdf or .doc and have them displayed as images on the webpage. I'm curious if there's a way to achieve this without converting them to .jpg or .png using thir ...

Is jQuery the key to Masonry's stacking magic?

Hey there, I could really use some assistance with my website at this link: I thought jQuery Masonry would stack the objects closely together, but when I randomize the div boxes, there are large gaps between them. Can anyone explain why this is happening? ...

Is there a Joomla extension available that can display or conceal content upon clicking?

Looking to enhance my Joomla site by installing a plugin that allows me to toggle the visibility of content with a click, similar to how FAQ sections work on many websites. Question 1 (click here for the answer) { Details for question 1 go here } Questi ...

Why isn't my Angular directive able to access the parent controller's scope within its isolated scope?

I have meticulously followed the Angular documentation to implement a directive with an isolated scope that contains a few variables from the parent Controller's scope object. app.controller('MainCtrl', function($scope) { $scope.name = ...