If a checkbox is checked, other checkboxs will be disabled


I have the following code. I now want to check the box if the checkbox is checked and disabled other checkboxes until the check box is cleared.

 <div id="gallery">
                        <input type="checkbox" name="ChekboxDefault" value="" />

                    <br />
                    <button type="button" onclick="Addchecked()">add checked</button>
                    <br />
    function Addchecked() {
        $('#gallery').append("<br/><span>     <input type='checkbox' name='ChekboxDefault' value='' /> </span>")



Hi @shabzadeh97, what have you tried so far to make this happen?


Give all the checkboxes the same classname, and use the .not($(this)) selector to disable all the other checkboxes.


^ _ ^


Sounds like you’re looking for radio buttons?


I create a number of dynamic checkboxes and I ask the user to select one, but the number of these check boxes is unknown.


I think your method is responsive. But the problem is how can I create dynamic data for value.

    function Add() {
          $('#gallery').append("<br/><span>    <input type='radio'  name='contact' value="+t+"> </span>")



I used the following solution

I add this code to html code

  <span id="counter" hidden="hidden"></span>

and then


    function Add() {
        var count = Number($('#counter').html());
        count = count + 1;
        $('#counter').html(count + 1);
        $('#gallery').append("<br/><span>    <input type='radio'  name='contact' value="+count+"> </span>")



You are increasing the value of count twice in your function. Did you intend to do that?


^ _ ^


Adding one is done so much in programming there is a shorthand for it. Instead of count = count + 1 you can use count++ :slight_smile:


I corrected both mistakes . Thanks ScallioXTX ,WolfShade


Hi @shabzadeh97, there is no reason to store that value in the markup though – you can just use a regular variable for the counter:

var $gallery = $('#gallery')
var count = 0

$('.my-button').click(function addChecked () {
  $gallery.append('<input type="radio" name="contact" value="' + count + '" />')

closed #12

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.