Is there a way to deactivate all the checkboxes mentioned above if the "None of the above" checkbox is selected?

While browsing websites, I noticed that some have checkbox options for:

Family: Wife Husband Son Daughter None. If "None" is selected, the rest of the options are disabled.

Answer №1

Feel free to utilize this code - hopefully, it proves beneficial for your needs!

Thank you.

<input type="checkbox" name="check" value="father" class="group1">Father
<input type="checkbox" name="check" value="mother" class="group1">Mother
<input type="checkbox" name="check" value="son & daughter" class="group1">Son & Daughter
<input type="checkbox" name="check" value="none" id="none" class="group1">None

$(function(){
$("#none").on("click", function(){
if(this.checked) {
$("input.group1").attr("disabled", true);
} else {
$("input.group1").attr("disabled", false);
}
});
});

Answer №2

"use strict";
console.clear();

{
  document.addEventListener('click', e => {
    const target = e.target
    if (target.nodeName.toLowerCase() == 'input' && target.type == 'checkbox' && target.dataset.trigger && target.dataset.trigger == 'none') {
      Array.from(target.form[target.name]).filter(element => element != target).forEach(el => el.disabled = target.checked)
    }
  })
}
input:disabled + label {
  color: lightgray;
}
<form id="my-form">
  <input type="checkbox" id="my-form-relatives-wife" name="relatives">
  <label for="my-form-relatives-wife">Wife</label>
  <input type="checkbox" id="my-form-relatives-husband" name="relatives">
  <label for="my-form-relatives-husband">Husband</label>
  <input type="checkbox" id="my-form-relatives-son" name="relatives">
  <label for="my-form-relatives-son">Son</label>
  <input type="checkbox" id="my-form-relatives-daughter" name="relatives">
  <label for="my-form-relatives-daughter">Daughter</label>
  <input type="checkbox" id="my-form-relatives-none" name="relatives" data-trigger="none">
  <label for="my-form-relatives-none">None</label>
</form>

Here is a backward compatible version

"use strict";
console.clear();

{
  document.addEventListener('click', function(event) {
    var targetElement = event.target
    if (targetElement.nodeName.toLowerCase() == 'input' && targetElement.type == 'checkbox' && targetElement.dataset.trigger && targetElement.dataset.trigger == 'none') {
    Array.prototype.filter.call(targetElement.form[targetElement.name], function(element) { return element != targetElement }).forEach(function(element) { return element.disabled = targetElement.checked})
    }
  })
}
input:disabled + label {
  color: lightgray;
}
<form id="my-form">
  <input type="checkbox" id="my-form-relatives-wife" name="relatives">
  <label for="my-form-relatives-wife">Wife</label>
  <input type="checkbox" id="my-form-relatives-husband" name="relatives">
  <label for="my-form-relatives-husband">Husband</label>
  <input type="checkbox" id="my-form-relatives-son" name="relatives">
  <label for="my-form-relatives-son">Son</label>
  <input type="checkbox" id="my-form-relatives-daughter" name="relatives">
  <label for="my-form-relatives-daughter">Daughter</label>
  <input type="checkbox" id="my-form-relatives-none" name="relatives" data-trigger="none">
  <label for="my-form-relatives-none">None</label>
</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

How can the timezone setting be utilized in a datetimepicker?

I recently started using Datetimepicker for date and time input, but I'm a bit confused about the timezone option. How does it affect the data submission to a datetime datatype in MySQL and what changes can I expect when selecting back the data? Any i ...

Is Jquery Mobile's Table lacking responsiveness?

I have implemented a basic table from the jQuery Mobile website on my page. Take a look at the HTML code below: <div data-role="page" id="mainPage"> <div data-role="content> <table data-role="table" id="my-table" da ...

Executing an Ajax call before navigating away from a webpage or closing it by utilizing the beforeunload

I have implemented a function using $(window).bind("beforeunload", function(e) {}) to trigger an ajax call before the browser is closed. During testing on my local machine, this function works perfectly fine. It successfully performs the AJAX call when e ...

Ways to efficiently populate HTML elements with JSON data

I am working on grasping the concept of functional programming. My understanding so far is that it involves encapsulating everything into functions and passing them around. For instance, in my current example, I am attempting to fetch data from a RESTApi a ...

What is the method for including a hyperlink in an swf document?

I've been working with HTML and trying to embed links in my webpages that are in offline mode. After doing some research, I came across the following code snippet: MyClickTagButton.addEventListener( MouseEvent.CLICK, function():void { if (roo ...

How to modify the content type in an Angular.js $http.delete request

When making a $http.delete request in my Angular app, I include a config object using the following approach: return $http.delete('projects/' + projectID + '/activityTypes', {data: [{id: 2}]}) This method attaches the values from my d ...

Challenges encountered when attempting to send an array in res.json with the MERN stack

I am facing a challenge while trying to make two separate model calls using the MERN stack. The issue arises when I try to send an array in res.json; extracting the data seems to be problematic. On examining the console log: {data: "[]", status: ...

Transforming a grid of radio buttons into a numerical representation or percentage

I am facing an issue with calculating the total value of radio button selections in three rows. Each row belongs to a specific group, and I want the value of each selection to appear in the last column. The first radio button in each row has a value of 2, ...

Bootstrap Popover not displaying information after an AJAX request

I'm struggling to update the popovers contents with Ajax result in my ASP.Net MVC4 project. Using ASP.Net (MVC4): public ActionResult GetEmployeeDetails(string employeeId) { var contract = UnitOfWork.ContractRepository.ContractBu ...

Show an image in a specific location on the webpage using JavaScript/jQuery

Is there a way for me to show a visual element at specific coordinates on the browser screen? For example, let's say I have calculated dynamic offsets of { left: 336, top: 378 }, and I would like to display a small line or image at that position. Is ...

Utilizing NPM Package Configuration Variables with Docker Commands: A Guide

I have a unique file structure where my package.json contains a single variable called settings which defines the port for the application: package.json ... "settings":{ "port": "3000" }, ... In addition, I've set up a custom script to execute a ...

Troubleshooting: Issues with Visual Composer and Jquery Animations not Functioning Properly

Having a visual composer installed on my Wordpress site, I encountered an issue where inserting an image or any element and applying animation from within visual composer would cause the page to go blank with no animations showing. After reaching out to t ...

The React Native Android app encountered an error loading the JS bundle: updates made to index.android.js are not reflecting in the generated APK file

Setting up a react-native android project on my Windows machine has been successful so far. To run the project, I use the command react-native run-android and then the installed apk is generated on my phone. After making some changes in the index.android. ...

JSON data returned from an AJAX request could be utilized to populate a

Need help with displaying data in two dependent select boxes based on the selection of the first box. <?php $con=mysql_connect("localhost","root","root"); mysql_select_db("register",$con); ?> <!DOCTYPE html> <html&g ...

Arrow function utilized in string rendered component

Hello everyone, I could use some help with the following code snippet: rowRenderer = (e) => { debugger; return e.data?.fileName ? '<a class="documentLink" onclick={() => \'' + console.log('\ ...

The Bootstrap collapse functionality is malfunctioning

Bootstrap is not my strong suit. I'm struggling with getting the collapse effect to work properly. Can anyone provide some guidance? Here is the link to the bootstrap I am using: <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/boo ...

How to showcase a single JSON object using AngularJS

When I retrieve news with a specific id from my REST API, using ng-repeat to display all news works fine. However, when I try to display a single object, this method does not work. My route code: .when('/displaynews/:id',{ templateUrl: &apo ...

What is the recommended way to initiate the first server-side external API query in Nuxt 3 and where should it be done?

I have a question regarding the process of developing web applications in Nuxt 3 using the composition API. I am working on an application that requires user location-dependent content. To achieve this, I need to determine the user's IP address using ...

What is the best approach for testing a component that makes use of React.cloneElement?

My main component fetches children using react-router in the following manner: class MainComponent extends Component { render() { return ( <div> {React.cloneElement(children, this.props.data)} </div> ) } } I a ...

What steps do I need to take to ensure that this Regex pattern only recognizes percentages?

I am attempting to create a specific scenario where I can restrict my string to three digits, followed by a dot and two optional digits after the dot. For example: 100.00 1 10.56 31.5 I've developed a regex pattern that allows me to filter out any ...