Tips on utilizing radio buttons in place of check boxes

How can I create a dynamic menu with multiple categories and submenus that toggle open and close when clicked? I currently have a menu structure with "What's New" and "Categories", and within the "Categories" section, I have three main categories each with their own submenu. My goal is to make it so that only one category or submenu is open at a time, meaning if a new category is clicked, the previous one automatically closes. I tried using radio buttons instead of checkboxes but encountered issues. Does anyone have a solution or suggestion for achieving this functionality? Any help would be appreciated. You can find the current structure and code in this Fiddle.

<div class='shop-sidebar'>
  <ul class='shop-nav'>
    <li class="active"><a href="#">What's New</a></li>
    <li class='w-sub' data-id='shop-categories'>
      <svg class='s_arrow_down'><use xlink:href="#s_arrow_down"></use></svg>
      <input type="checkbox" id="categories" />
      <label id="label" for="categories">Categories</label>
      <ul id="subList">
        <li>
          <input type="checkbox" id="all" />
          <label id="allLabel" for="all">All</label>
        </li>
        <li>
          <input type="checkbox" id="category1" />
          <label id="category1Label" for="category1">Category 1</label>
          <ul id="subListCategory1">
            <li><a href="#">All</a></li>
            <li><a href="#">Sub Category 1</a></li>
            ...
          </ul>
        </li>
        <li>
          <input type="checkbox" id="category2" />
          <label id="category2Label" for="category2">Category 2</label>
          <ul id="subListCategory2">
            <li><a href="#">All</a></li>
            ...
          </ul>
        </li>
        <li>
          <input type="checkbox" id="category3" />
          <label id="category3Label" for="category3">Category 3</label>
          <ul id="subListCategory3">
            <li><a href="#">All</a></li>
            ...
          </ul>

        </li>

      </ul>
    </li>

  </ul>
</div></code></pre></div></div></p>

<p>css</p>

<p><div>
<div>
<pre class="snippet-code-css lang-css"><code>.shop-sidebar {
width: 30%;
width: calc(295px);
...
}

Answer №1

To create a group of radio buttons, change the checkboxes to radio buttons and give them all the same name attribute. This way, only one radio button can be checked at a time.

     <ul id="subList">
        <li>
          <input type="checkbox" id="all" />
          <label id="allLabel" for="all">All</label>
        </li>
        <li>
          <input type="radio" name= "category"  id="category1" />
          <label id="category1Label" for="category1">Category 1</label>
          <ul id="subListCategory1">
            <li><a href="#">All</a></li>
            <li><a href="#">Sub Category 1</a></li>
            <li><a href="#">Sub Category 2</a></li>
            <li><a href="#">Sub Category 3</a></li>
            <li><a href="#">Sub Category 4</a></li>
            <li><a href="#">Sub Category 5</a></li>
            <li><a href="#">Sub Category 6</a></li>
            <li><a href="#">Sub Category 7</a></li>
            <li><a href="#">Sub Category 8</a></li>
            <li><a href="#">Sub Category 9</a></li>
          </ul>
        </li>
        <li>
          <input type="radio" name= "category"  id="category2" />
          <label id="category2Label" for="category2">Category 2</label>
          <ul id="subListCategory2">
            <li><a href="#">All</a></li>
            <li><a href="#">Sub Category 1</a></li>
            <li><a href="#">Sub Category 2</a></li>
          </ul>
        </li>
        <li>
          <input type="radio" name= "category" id="category3" />
          <label id="category3Label" for="category3">Category 3</label>
          <ul id="subListCategory3">
            <li><a href="#">All</a></li>
            <li><a href="#">Sub Category 1</a></li>
          </ul>

        </li>

  </ul>

Make sure to add the CSS code to hide the radios:

input[type='radio'] {
  display: none;
}

Check out the updated CSS code:

.shop-sidebar {
width: 30%;
width: calc(295px);
display: inline-block;
padding-right: 65px;
vertical-align: top;
font-family: 'maison',sans-serif;
font-weight: 600;
font-size: 11px;
color: #000;
letter-spacing: 1.5px;
line-height: 18px;
text-transform: uppercase;
}
ul.shop-nav {
list-style: none;
padding: 0;
margin: 0;
}
ul.shop-nav li.active, ul.shop-nav li:hover {
color: #000;
opacity: 1;
font-weight: bold;
}
ul.shop-nav li {
transition: all 0.3s;
cursor: pointer;
padding: 18px 20px;
background-color: #f8f8f8;
margin-bottom: 2px;
}
ul.shop-nav li.active a {
color: #000;
}
ul.shop-nav a {
color: #000;
}
ul.shop-nav li.active, ul.shop-nav li:hover {
color: #000;
opacity: 1;
font-weight: bold;
}
ul.shop-nav li svg {
width: 10px;
height: 10px;
vertical-align: text-bottom;
fill: #000;
transition: all 0.3s;
float: right;
}
ul.shop-nav li ul {
display: none;
list-style: none;

...

#category3:checked ~ #subListCategory3 {
  display: block;
}
<div class='shop-sidebar'>
  <ul class='shop-nav'>
    <li class="active"><a href="#">What's New</a></li>
    <li class='w-sub' data-id='shop-categories'>
      <svg class='s_arrow_down'><use xlink:href="#s_arrow_down"></use></svg>
      <input type="checkbox" id="categories" />
      <label id="label" for="categories">Categories</label>
      <ul id="subList">
        <li>
          <input type="checkbox" id="all" />
          <label id="allLabel" for="all">All</label>
        </li>
        <li>
          <input type="radio" name= "category"  id="category1" />
          <label id="category1Label" for="category1">Category 1</label>
          <ul id="subListCategory1">
            <li><a href="#">All</a></li>
            <li><a href="#">Sub Category 1</a></li>

...

        </li>

      </ul>
    </li>

  </ul>
</div>

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

Storing various duplicates of items in local storage

Looking for help with storage settings in HTML/JavaScript as I work on a mobile not taking app using Phonegap. My goal is to allow users to input a note name and content, save them into a jquery mobile list, and see them on the home screen. However, I&apos ...

Eliminate a specific element from the dataset

My question revolves around a data array used to populate three drop down <select> boxes. How can I eliminate duplicate values within the drop downs without affecting the array itself? For example: data = { firstbox_a: ['grpA_1','gr ...

Is there a way to shift this modal to the right?

I am facing an issue with aligning a button modal to the right side. I attempted using bootstrap classes like : float:right, mt:20 Unfortunately, these did not have the desired effect. Below is my code snippet: <b-modal ref="my-modal" hide-fo ...

"Troubleshooting: How to Fix Issues with document.getElementById on Dynamic Div

Struggling to incorporate div elements and generate graphs with Google charts? The issue arises in the draw function, where attempts to access a div element using document.getElementById() result in null values and an error message stating "container not ...

Ways to split images and text in list items using CSS

Can the text be formatted in a specific location on the page, separate from the image and heading? This is the HTML code I am currently using: <div class="tab-pane container fade" id="environmental"> <div class="row"> <div class="c ...

Servlet unable to retrieve parameters from Form due to unexpected NULL values

Here is a basic form element for you to review: <form action="Test" method="POST" enctype="multipart/form-data"> <input type="text" name="first_name" title="First Name"></input> <input type="text" name="last_name" title="Las ...

Move the text to the following line if a horizontal scroll bar is visible using JavaScript/JQuery and CSS styling

I have a section with multiple div elements...how can I ensure that when there is horizontal scrolling in the section, the hidden divs shift to the next line? HTML <section id="a"> <div class="num"> <div class="num"> <div class="num" ...

Utilize Bootstrap4 to position content above the navigation icon

I need assistance to modify my code so that it will have the following appearance: I successfully created a navigation bar icon and inserted the logo, but I am struggling to position the data above the navigation icon: My current code: <nav class="n ...

Can you suggest the best HTML5 structure for a multi-chapter novel?

In the process of creating a small HTML5 book with chapters and sections, I encountered a dilemma when it comes to structuring my documents like this: chapter1.html - introduction to chapter 1 chapter1section1.html - section 1.1 chapter1section2.ht ...

Creating a dynamic table using jQuery and XML content

Hello everyone, I am new to jQuery and JavaScript in general. My goal is to generate a table based on XML data, but I'm struggling to achieve the correct output. Here's what I have tried so far: Here is my HTML code: <table id="daily_fruit"& ...

Different ways to modify the color and thickness of a variable in HTML5 with either JavaScript or CSS

I am currently working with a JavaScript file where I have a variable defined as follows: var nombre = document.getElementById('nombre').value; The 'nombre' variable corresponds to an element in an HTML file: Nombre: <input type=" ...

Is there a definitive way to distinguish between scrollTop and scrollHeight in web development?

For instance, function checkingScroll(){ var newHeight = element.scrollHeight; var scrollTopValue = element.scrollTop; alert("The scrollHeight property is: " + newHeight + "px"); alert("The scrollTop property is: " + scrollTopValue ...

Kohana ajax causing removal of JQuery Data attributes

Currently, I am developing an application where jquery data is used to pass variables to HTML elements. It has been successful in one part of the site when the data attributes are added to a tr tag. The following code works: <tr class="js-instructions ...

Choose all of the IDs from various sections using Jquery

I am facing an issue with having 2 identical IDs inside 2 separate sections. When the button is clicked, I want it to select the ID within this section and apply a style, then also select the ID in the other section and apply that same style. The code sni ...

What is the best way to incorporate custom CSS into an angular-fullstack application?

I'm interested in incorporating an image into a jumbotron and adjusting its width correctly, similar to what was discussed in this question: Bootstrap image to jumbotron width However, I am unsure of where to place my custom CSS code to achieve the d ...

optimal application of css with jquery

I have a question about using jQuery to set the padding of a class or id. I am able to successfully change the height of an element with this code: $('.menu').css({ height: '90px' }); But now, I need to apply a specific CSS rule in jQ ...

Bootstrap 5 - Achieve automatic column width within a row

In my Bootstrap 5 setup, I have a variety of pages that have different column layouts - sometimes 2 columns, sometimes 3. The template system I'm using dynamically adjusts the layout to accommodate the need for a third column when necessary. <div ...

Issues arise with the Dropend Menu in Bootstrap when attempting to utilize a dropdown menu

As I work on developing a sidebar using Bootstrap 5, I encountered an issue with the positioning of a "Dropdown" and "Dropend" menu when they are open. Specifically, while the Dropdown menu functions correctly, the Dropend menu does not align properly. In ...

Tips for positioning a chat box at the bottom of a v-card's visible area

My goal is to create a chat app using Vuejs 3 and Vuetify 3, but I'm encountering an issue aligning the chatbox with the v-card component. Instead of being positioned at the bottom of the v-card, the chatbox (green) appears at the bottom of the page. ...

A guide on implementing gradient color fill for scatter plot points within Google Charts

I am interested in creating a scatter plot using a data set that includes three series: (1) for the x-axis, (2) for the y-axis, and (3) a third series that contains only 0 and 1 values. For this plot, I would like the points with a third series value of 0 ...