Search code examples
javascriptjqueryjspjstl

Enable/disable dropdowns when checkbox checked/unchecked


I'm creating a web app using Java servlets and JSP. I have a table which contains songs. Every song has its own checkbox and drop-down. like this:

table

You can see that taken songs have their checkbox and drop-down disabled and that's how I want it.

What I want is when the page loads all the drop-downs will be disabled, and when I check one of the available checkboxes the drop-down that is in the same line will be enabled and when I uncheck that checkbox the dropdown will be disabled again.

Here is the JSP code:

<table id="myTable"> 
    <tr>
    <th>Songs</th>
    <th>Selected</th>
    <th>#</th>
    <th>Available/Taken</th>
    </tr>
    <c:forEach items="${Entities}" varStatus="loop">
        <tr>
        <td><c:out value="${Entities[loop.index]}"/></td>               
        <td><input id="checkbox1" type="checkbox" name="selected" value=" ${Entities[loop.index]}" ${checkboxDis[loop.index]} ><br></td>
        <td>
            <select name="myselect" id="drop" disabled >
                <option selected disabled>#</option>
                <option>Cmaj</option><option>Gmaj</option>
                <option>Dmaj</option><option>Amaj</option>
                <option>Emaj</option><option>Bmaj</option>
                <option>Fmaj</option><option>Bb_maj</option>
                <option>Eb_maj</option><option>Ab_maj</option>
                <option>Db_maj</option><option>Gb_maj</option>
                <option>Amin</option><option>Emin</option>
                <option>Bmin</option><option>F#min</option>
                <option>C#_min</option><option>G#_min</option>
                <option>D#_min</option><option>Cb_maj</option>
                <option>Gmaj</option><option>Dmaj</option>
                <option>F#maj</option><option>Cmaj</option>
                <option>Fmaj</option><option>Bb_maj</option>
                <option>Eb_maj</option><option>Ab_maj</option>
                <option>Db_maj</option><option>A#</option>
            </select>
        </td>
        <td>
        <c:choose>
        <c:when test="${checkboxDis[loop.index].equals('disabled')}">
            <i class="fa fa-ban" style="color:red;"></i>
        </c:when>
        <c:when test="${checkboxDis[loop.index].equals('')}">
            <i class="fa fa-check-circle" style="color:green;"></i>
        </c:when>
        </c:choose>
        </td>
        </tr>   
    </c:forEach>
</table>

The jQuery code I've done so far:

var selected1 = new Array();

$(document).ready(function() {
  $("input[type='checkbox']").on('change', function() {
    if ($(this).is(":checked")) {
      selected1.push($(this).val());
      for (var i = 0; i < selected1.length; i++) {
        if (selected1[i] == $(this).val()) {
          $('#drop')[i].prop("disabled", false);
        }
      }
    } else {
      for (var i = 0; i < selected1.length; i++) {
        if (selected1[i] == $(this).val()) {
          selected1.splice(i, 1);
          $('#drop')[i].prop("disabled", true);
        }
      }
    }
  });
});

The code isn't working because I don't know how to handle the drop-downs since they have the same names and ids.


Solution

  • Your IDs need to be unique - in this case not needed at all for enabling the select

    $(function() {
      $("input[type='checkbox']").on('change', function() {
        $(this).closest("tr").find("select[name=myselect]").prop("disabled", !this.checked)
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table>
      <tr>
        <td><input id="checkbox_1" type="checkbox" name="selected" value="1"></td>
        <td>
          <select name="myselect" id="drop_1" disabled>
            <option selected disabled>#</option>
            <option>Cmaj</option>
            <option>Gmaj</option>
            <option>Dmaj</option>
            <option>Amaj</option>
            <option>Emaj</option>
            <option>Bmaj</option>
            <option>Fmaj</option>
            <option>Bb_maj</option>
            <option>Eb_maj</option>
            <option>Ab_maj</option>
            <option>Db_maj</option>
            <option>Gb_maj</option>
            <option>Amin</option>
            <option>Emin</option>
            <option>Bmin</option>
            <option>F#min</option>
            <option>C#_min</option>
            <option>G#_min</option>
            <option>D#_min</option>
            <option>Cb_maj</option>
            <option>Gmaj</option>
            <option>Dmaj</option>
            <option>F#maj</option>
            <option>Cmaj</option>
            <option>Fmaj</option>
            <option>Bb_maj</option>
            <option>Eb_maj</option>
            <option>Ab_maj</option>
            <option>Db_maj</option>
            <option>A#</option>
          </select>
        </td>
      </tr>
      <tr>
        <td><input id="checkbox_2" type="checkbox" name="selected" value="2"></td>
        <td>
          <select name="myselect" id="drop_2" disabled>
            <option selected disabled>#</option>
            <option>Cmaj</option>
            <option>Gmaj</option>
            <option>Dmaj</option>
            <option>Amaj</option>
            <option>Emaj</option>
            <option>Bmaj</option>
            <option>Fmaj</option>
            <option>Bb_maj</option>
            <option>Eb_maj</option>
            <option>Ab_maj</option>
            <option>Db_maj</option>
            <option>Gb_maj</option>
            <option>Amin</option>
            <option>Emin</option>
            <option>Bmin</option>
            <option>F#min</option>
            <option>C#_min</option>
            <option>G#_min</option>
            <option>D#_min</option>
            <option>Cb_maj</option>
            <option>Gmaj</option>
            <option>Dmaj</option>
            <option>F#maj</option>
            <option>Cmaj</option>
            <option>Fmaj</option>
            <option>Bb_maj</option>
            <option>Eb_maj</option>
            <option>Ab_maj</option>
            <option>Db_maj</option>
            <option>A#</option>
          </select>
        </td>
      </tr>
      </table>