Disabling radio button based on user selection

Viewed 56

I have 2 sets of radio buttons with 4 options each. These 4 options are the same in both categories. I do not want to allow same selection to be possible in both categories.

For example - if option 1 is chosen is in the first category, the user can now only select from option 2/3/4 in the second category.

This question has been asked before, but nothing seems to be working for my case. Here is my code -

$("input[type=radio]").click(function() {
  
  $("input[type=radio][value=" + $(this).val() + "]").attr("disabled", "disabled");
  $(this).removeAttr("disabled");
});
table {
  border-collapse: collapse;
}

td,
th {
  border: 1px solid #ccc;
  padding: 10px;
}

th:empty {
  border: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <th>Most Supportive</th>
    <th>Reddit comment</th>
    <th>Most Offensive</th>
  </tr>
  <tr>
    <td><input type="radio" name="Most1" id="Most-1a" data-min-check="1" required value="a" data-col="1"></td>
    <td>Comment 1</td>
    <td><input type="radio" name="Least1" id="Least-1a" data-min-check="1" required value="a" data-col="1"></td>
  </tr>
  <tr>
    <td><input type="radio" name="Most1" id="Most-1b" data-min-check="1" required value="b" data-col="2"></td>
    <td>Comment 2</td>
    <td><input type="radio" name="Least1" id="Least-1b" data-min-check="1" required value="b" data-col="2"></td>
  </tr>
  <tr>
    <td><input type="radio" name="Most1" id="Most-1c" data-min-check="1" required value="c" data-col="3"></td>
    <td>Comment 3</td>
    <td><input type="radio" name="Least1" id="Least-1c" data-min-check="1" required value="c" data-col="3"></td>
  </tr>
  <tr>
    <td><input type="radio" name="Most1" id="Most-1d" data-min-check="1" required value="d" data-col="4"></td>
    <td>Comment 4</td>
    <td><input type="radio" name="Least1" id="Least-1d" data-min-check="1" required value="d" data-col="4"></td>
  </tr>
</table>

1 Answers

First enable all radio buttons, then disable the one you would like to disable:

$("input[type=radio]").click(function() {
  console.log("here")
  $("input[type=radio]").removeAttr("disabled");
  $("input[type=radio][value=" + $(this).val() + "]").attr("disabled", "disabled");
  $(this).removeAttr("disabled");
});
table {
  border-collapse: collapse;
}

td,
th {
  border: 1px solid #ccc;
  padding: 10px;
}

th:empty {
  border: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <th>Most Supportive</th>
    <th>Reddit comment</th>
    <th>Most Offensive</th>
  </tr>
  <tr>
    <td><input type="radio" name="Most1" id="Most-1a" data-min-check="1" required value="a" data-col="1"></td>
    <td>Comment 1</td>
    <td><input type="radio" name="Least1" id="Least-1a" data-min-check="1" required value="a" data-col="1"></td>
  </tr>
  <tr>
    <td><input type="radio" name="Most1" id="Most-1b" data-min-check="1" required value="b" data-col="2"></td>
    <td>Comment 2</td>
    <td><input type="radio" name="Least1" id="Least-1b" data-min-check="1" required value="b" data-col="2"></td>
  </tr>
  <tr>
    <td><input type="radio" name="Most1" id="Most-1c" data-min-check="1" required value="c" data-col="3"></td>
    <td>Comment 3</td>
    <td><input type="radio" name="Least1" id="Least-1c" data-min-check="1" required value="c" data-col="3"></td>
  </tr>
  <tr>
    <td><input type="radio" name="Most1" id="Most-1d" data-min-check="1" required value="d" data-col="4"></td>
    <td>Comment 4</td>
    <td><input type="radio" name="Least1" id="Least-1d" data-min-check="1" required value="d" data-col="4"></td>
  </tr>
</table>

Related