Search code examples
jquerychecked

How to disable when checkbox checked


I have few jQuery tags in one page for sample…

Js/jquery.js
Js/jquery-2.js
Js/jquery-3.js

Due to crashed each other at the same time and won’t work to use all of those .js tags same time.

I have created 3 checkboxes like this…

<input name="jquery" type="checkbox" value="jquery">
<input name="jquery-2" type="checkbox" value="jquery-2">
<input name="jquery-3" type="checkbox" value="jquery-3">

What I like to have is when checked any of those checkbox and is become disable, so for sample…. I checked for jquery and jquery-2, and leave un-check jquery-3.

My question is that how can write code to disable jquery from the file when checked.

Many thanks.


Solution

  • Assuming you want the checkboxes to behave in a group (selecting 1 unchecks all others), you can do something like:

    let checkBoxes = $("[name^=jquery]")
    
    // Listen to changes on all checkBoxes, ignore the current element and uncheck all else
    // Make sure to use jQuery 1.6+
    checkBoxes.change(function(e) {
        const me = this;
        checkBoxes.not(me).prop('checked', false).removeAttr('checked')
    })