Streamlining input options with a single label for multiple selections (radio buttons)

$('label').click(function() {
  id = this.id.split('-');

  if (id[0] === '1') {
    id[0] = '2';
  } else {
    id[0] = '1';
  }

  $('#' + id[0] + '-' + id[1]).prop('checked', true);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="one">
  <input type="radio" id="1-1" name="1-level">
  <label for="1-1" id="1-1">1</label>
  <input type="radio" id="1-2" name="1-level">
  <label for="1-2" id="1-2">2</label>
</div>
<div class="two">
  <input type="radio" id="2-1" name="2-level">
  <label for="2-1" id="2-1">1</label>
  <input type="radio" id="2-2" name="2-level">
  <label for="2-2" id="2-2">2</label>
</div>

Can one label be used for multiple radio inputs? For example, when clicking on a label with an ID of "1", can it check two input fields (with IDs of 1 in the divs with classes "one" and "two") at the same time? Here is an example:

<div class="one">
    <input type="radio" id="1" name="level">
    <label for="1">1</label>
    <input type="radio" id="2" name="level">
    <label for="2">2</label>
</div>
<div class="two">
    <input type="radio" id="1" name="level">
    <label for="1">1</label>
    <input type="radio" id="2" name="level">
    <label for="2">2</label>
</div>

Answer №1

A reference from the documentation can be found at: https://www.w3.org/TR/html401/interact/forms.html#h-17.9.1

The LABEL element serves the purpose of providing information to controls. Each LABEL element is linked with precisely one form control.

Therefore, the only way to achieve what you are requesting is by utilizing JavaScript

HTML:
Replace id with data-input-group-id and for with data-for-input-group. Additionally, now you have the flexibility to use "1" and "2" instead of "1-1" and similar variations.

JS:

$('[data-for-input-group]').click(function() {
   let inputGroupId = this.dataset.inputGroupId;
   $('[data-input-group-id = "'+inputGroupId+'"]').prop('checked', true);
});

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

jQuery Ajax Load() causing screen to refresh unintentionally upon clicking

I am currently developing a web application that includes side menus. However, I am facing an issue with the functionality of these menus. Whenever I click on certain options, the entire page refreshes. For example, under my main menu "Planning the Engagem ...

Is it feasible to add to an ID using ngx-bootstrap's dropdown feature?

In the documentation for ngx dropdown, there is a feature called "append to body." I recently tried changing this to append to a table element instead and it worked successfully. Now, on another page, I have two tables displayed. If I were to assign each ...

Dynamically changing CSS properties

http://jsfiddle.net/U6gWx/1/ $("div.ttt").css('display','block'); There is a filter that might empty the first column, which is set to float:left. When the first column is empty, I want to update the second column's CSS style to ...

Unwanted overlay issues with CSS positioning

My webpage has a header div, with two nested divs called header-left-box and header-right-box that are floated left and displayed side-by-side horizontally. Following these divs, I want to include a receiver div that occupies the free space below. The is ...

Is it possible to create clickable titles in highcharts that also interact with bootstrap popovers?

Hello, I'm trying to figure out how to turn the title in highcharts into a clickable link that works with bootstrap's popover feature. You can find an example of what I'm working on here: http://jsfiddle.net/287JP/4/ $(function () { ...

Element child's attribute value not properly implementing Polymer's two-way data binding

I have a product-card custom element with the following structure: <dom-module id="product-card"> <template> <div class="card-main-content layout horizontal center"> <content select="img"></content> < ...

Expanding Viewports with Dynamic Scrolling Bootstrap Columns

Striving to achieve a specific design in Bootstrap, but hitting roadblocks. The criteria are as follows: No scrolling in the browser window. Display occupying 100% of browser height. Columns for Menu, Left contents, and Right contents must scroll indepen ...

Ensure that the <aside> elements span the entire width of their containing parent element

I am currently designing a website and I am aiming for a specific layout. Any advice on how to achieve this would be greatly appreciated. The header of the page consists of: a logo aligned to the left within an <a> tag 2 widgets placed in <asid ...

The dividers flicker in and out of view

I have a menu with 7 elements, where clicking on an element causes its content to appear by fading in. If another element is clicked, the current content fades out and the new content fades in. I've successfully implemented this concept for 3 of the 7 ...

Large background images on websites often appear pixelated

Check out my website: I've noticed that the background image appears blurry on my screen, despite its size being 1920 x 1080 px. I've tried using a jquery script to manage this background issue as all of the CSS methods have provided unsatisfact ...

Robotic Arm in Motion

GOAL: The aim of the code below is to create a robotic arm that consists of three layers (upper, lower, and middle), all connected to the base. There are four sliders provided to independently move each part except for the base which moves the entire arm. ...

Submitting a form into a database with the help of AJAX within a looping structure

Trying to input values into the database using AJAX. The rows are generated in a loop from the database, with each row containing a button column. Clicking on the button submits the values to the database successfully. The issue is that it only inserts va ...

The alignment of table rows does not match the table header in Internet Explorer

Encountering an issue while using NgTableParams to display a table in IE. The table displays correctly in Chrome, but the alignment of rows is off in IE. Check out the demo here When you open the above link in IE and launch the preview window in a separat ...

What is the best way to display cards next to each other on desktop and mobile devices while maximizing available space?

In the world of React components, there exists a card representation within a grid view that adapts based on available screen space; function EngineerCard(engineer: EngineerDetail) { return ( <div className='engineerCard'> <d ...

What is the process for switching directories and renaming a file when uploading in nodeJs?

I am currently using multer and fs to handle the upload of an image file. How can I modify the directory where uploaded files are stored? Currently, all files are saved in my "routes" folder instead of the "uploads" folder created by multer. Additionally, ...

Trouble with the x-cloak attribute in alpine.js

Experience with TailwindCSS and AlpineJS in my current project has brought to my attention a slight issue with the header dropdowns flashing open momentarily when the login page loads. I attempted to use x-cloak to address this, but encountered some diffic ...

How come Bootstrap isn't functioning despite being properly loaded in my Django project?

I recently started working on a Django project and encountered some confusion while integrating Bootstrap3 for the front-end. After successfully installing django-bootstrap3 using pip in my command prompt, I added it as a third-party app in my settings.py ...

Having trouble with your CSS dropdown menu?

I've been struggling to implement a dropdown menu on my website following an online tutorial. Despite following the steps, I can't seem to get it working. It's frustrating because I want to move on to backend development. Hoping someone can ...

Will linking to /file.php in an anchor tag always redirect to http://thedomain.com/file.php?

After conducting my testing, I have found that this holds true. However, I want to confirm whether this is a standard behavior across all browsers or just a coincidence in the ones I have tested. In essence, I have a page located at: http://domain.com/s ...

One-click process succeeds where two clicks fail

The code below is intended to go through a two-click process as follows: First click on .imagenes decreases opacity and makes .logo visible. Second click on .imagenes returns the opacity to 1 and hides .logo again. However, during this cycle of two ...