Restricting the number of checkboxes using jQuery depending on which radio buttons are selected

Having an issue with checkboxes on a webform. The user must choose options for a purchase by selecting checkboxes and radio buttons. There are 2 radio buttons, numbered 1 and 2, representing the quantity of the product to be purchased. Following those are about 5 checkboxes indicating different colors of the product. When a user selects 1 at the radio buttons, only 1 checkbox should be active; if they select 2, then only 2 checkboxes should be available to check. Looking for help, potentially using jQuery for this task...

The setup includes two divs: one with the id #radioButtons containing the 2 radio buttons and another div with the remaining checkboxes.

Answer №1

Since there is no provided markup in your post, I will assume it would look something like this:

HTML

<div id="radioButtons">
  <input type="radio" name="quantity" id="radio1">
  <input type="radio" name="quantity" id="radio2">
</div>
<div class="checkBoxes">
  Color 1:<input type="checkbox" name="color" disabled="disabled">
  Color 2:<input type="Checkbox" name="color" disabled="disabled">
  Color 3:<input type="CHECKBOX" name="color" disabled="Disabled">
  Color 4:<input type="CheckBox" name="color" Disabled="Disabled">
  Color 5:<input type="chEcKboX" name="color" DISABLED="DISABLED">
</div>

JQuery

$('#radio1').on('change', function() {
    $('.checkBoxes :checkbox').prop('disable', true);
    $(':check:eq(0)').prop('enable', false);
});

$('#radio2').on('change', function() {
    $('.checkBoxes :checkbox').prop('disable', true);
    $(':check:eq(0), :check:eq(1)').prop('enable', false);
});

Demo Link

Answer №2

Using the actual HTML source where the issue arises could be a potential solution.

UPDATE:

I am struggling to comprehend your reasoning behind wanting to pursue this approach. In terms of usability, this method seems rather unconventional and perhaps not ideal. Allow me to pose some questions for you.

  • What is the motivation behind implementing it in this manner?
  • What options do you wish to offer your users?

There are numerous ways to incorporate inline selections, such as the well-known example of selecting an iPad model on apple.com. However, the key is to offer user interfaces that are easily understandable and provide a pleasant user experience.

Answer №3

One approach is to attach a click event to the checkboxes and use an event handler to verify if the maximum number of checkboxes allowed has been reached. Based on this condition, you can return either true or false.

Answer №4

Check out this example

I am managing the creation of checkboxes based on the radio button's id and dynamically generating them within a div.

UPDATE:

I have made changes to the provided fiddle. Please see the updated version here: Updated Fiddle

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

Having an issue with receiving an "undefined variable" error message when trying to validate a form using PHP and AJAX

Upon loading my page, an error message appears: Notice: Undefined variable: titleErr in index.php on line 141 I have already initialized the $titleErr variable at the start of my PHP code, what could be causing this issue? PHP: <?php $titleE ...

Struggling to construct a binary tree as my descendants are not arranged in the right sequence

I am currently working on building a binary tree using PHP, MySQL, and a jQuery plugin developed by Frank-Mich. Here is the progress I have made so far... DATABASE STRUCTURE CREATE TABLE IF NOT EXISTS `members` ( `id` int(11) NOT NULL AUTO_INCREMENT, ...

"Implementing jquery autocomplete feature with the ability to fetch data from

Currently, I am utilizing jquery's ajax functionality to retrieve data from an external php file. The retrieved data is then intended for use in the autocomplete feature. However, instead of displaying individual values from the array in the php file ...

Real-time data updates using AJAX in Ruby on Rails

Currently, I am working with Rails and jquery to implement dynamic content using ajax. However, one issue I am facing is extracting the current user ID from the URL For instance, if the URL is www.mywebsite.com/users/20 In my javascript file, I require ...

Deciding on the proper character formatting for each individual character within the RICHT TEXT EDITOR

After browsing numerous topics on Stackoverflow, I was able to develop my own compact rich text editor. However, one issue I encountered is that when the mouse cursor hovers over already bold or styled text, it's difficult for me to identify the styl ...

Fill in datatable with information from a JSON array if there are missing values in certain columns

My goal is to populate a datatable in JavaScript. Currently, I am able to do so, but some of the last rows have blank columns which are populated first. I attempted to fill those blank columns, and then the data populates in order. Here is an example of my ...

Issues with the animation of the navbar menu button are preventing it from functioning

I have been attempting to incorporate animation when the navbar-button is toggled on smaller screen sizes. Inspired by the design of .navbar-toggle.larr in this particular template, I tried to implement a similar effect. /* ANIMATED LEFT ARROW */ .navbar- ...

Can you explain the purpose of App.hiddenDivs in jQuery code snippet provided?

Exploring various JQuery performance tips on this insightful website Do you happen to know the significance of App.hiddenDivs ? ...

Switching a Rails/Ajax form from create to update mode upon submission

While experimenting with a star ratings page, I encountered an issue where the form element remained in "Create" mode instead of updating to an "Update" method after submission. The rating form is ajaxified, but it lacks the functionality to dynamically sw ...

Updating the content of a div when the mouse hovers over it

Looking for some help here - I have a few divs with paragraphs of text inside. My goal is to change the text in each div when it's being hovered over. I know this can be done using JavaScript (jquery perhaps?), but my coding skills are pretty basic. A ...

Issue with change event not firing upon page load

In my production record creation page, I have implemented functionality to handle different line, shift, and date combinations. There are drop down menus for selecting the line and shift, as well as a jQuery date picker for selecting the date. One issue I ...

Having trouble with innerHTML.value functionality?

Recently, I've been delving into creating a JavaScript program that fetches Wikipedia search results. Initially, everything seemed to be working fine as I was able to display the searched item using the alert() method. However, for some reason, it now ...

how to change the color of a specific item in a list created using v-for

I'm a beginner when it comes to vueJs and I'm attempting to toggle the class "active" on a single element once it has been clicked. Currently, my code toggles all elements with the class material_icons. How can I modify it to toggle only the elem ...

Troubleshooting problem with retrieving Ajax data in Datatables

Hello, I'm currently working on implementing the datatables example and have run into a roadblock while trying to access sample data. You can view the fiddle where I'm having trouble here For reference, here is a working example of datatables f ...

"Troubleshooting problems with jQuery accordion collapse and styling using CSS

I'm currently dealing with an issue regarding my accordion design. I have customized the stylesheet using themeroller, but when I collapse one of the sections, the header changes to black instead of reverting back to its original red color. I've ...

JSP form continues to submit despite JavaScript validation returning false

On my JSP page, I have a form named "deleteCont" and a JavaScript function called "validateDelete". When the user clicks the "Delete contact" button, the "validateDelete" function is triggered successfully, showing an alert message. Unfortunately, even whe ...

Tips for altering the scrolling rate of a container

I am trying to adjust the scroll speed of specific divs among a group of 5 divs. I came across a solution that changes the scroll speed for the entire document: http://jsfiddle.net/36dp03ur/ However, what I really need is a scenario like this: <div i ...

I am attempting to modify the text of a specific button in jQuery using HTML, but I am encountering difficulties in making

I am having trouble updating the text of a specific button in HTML using jQuery. Here is my jQuery code: <script> $(document).on('click','.track',function() { var track_button_id = $(this).attr("id"); $.ajax({ ...

Several Divs Positioned on Different Pages with Backgrounds That "Scroll"

Currently utilizing: jQuery Mobile 1.4.5 I am interested in knowing if it is feasible to achieve the following, and if so, how to start working on it. At the moment, there are 4 page divs that utilize swipe for navigation between them with a horizontal t ...

In jqGrid's gridComplete event, we can use the getRowData method to retrieve the value of a

Seeking guidance on extracting variables from jqGrid getRowData method While iterating through rows, I simply want to retrieve the ID and Phrase column values into separate variables gridComplete: function () { var allRowsInGrid = $('#list'). ...