0

I have this interface, when I clicked the checkbox, the input will open

interface

I use JQuery for this function and the problem is, only first row is able to open when checked the checkbox. Below are my code:

html:

<div class="row">
      <div class="col">
        <table class="table table-bordered table-hover">
        <tr>
          <th >Name</th>
          <th >Age</th>
        </tr>
        @foreach($peoples as $people)
          <tr>
            <td >
              <input type="checkbox" id="name" name="name" value=""/>&nbsp;&nbsp;
              {{$people->name}}
            </td>
            <td>
              <input type="number" id="age" name="age" value="{{$people->age}}" disabled />
            </td>
          </tr>
        @endforeach

        </table>
      </div>
    </div>

JQuery

<script>
    $(document).ready(function () {
        $('#name').on('change', function () {
            $("#age").prop("disabled", !$(this).is(':checked'));
            $("#age").val('');
        });
    });

    </script>
2
  • This seems to repeat the same ID through a loop. This is the wrong way and you have to change the id every time the loop iterates over. Commented Mar 4, 2021 at 8:52
  • Once again - IDs must be unique - $("#age") will only reference the first one Commented Mar 4, 2021 at 9:09

1 Answer 1

1

This is because you are creating multiple HTML elements with the same ID. You can change those IDs to classes.

Dynamic approach

You can find the .age input that is in checkbox's parent <tr>:

@foreach($peoples as $people)
<tr>
    <td>
        <input type="checkbox" class="name" value=""/>&nbsp;&nbsp;
        {{$people->name}}
    </td>
    <td>
        <input type="number" class="age" value="{{$people->age}}" disabled />
    </td>
</tr>
@endforeach

And in the Javascript you find the .age input that belongs to the parent

$(document).ready(function () {
    $('.name').on('change', function () {
        let input = $(this).closest('tr').find('input.age')
        input.prop("disabled", !$(this).is(':checked'));
        input.val('');
    });
});

With row identifiers

You could also add an addition attribute to those inputs containing unique values of each one:

@foreach($peoples as $people)
    <tr>
    <td >
        <input type="checkbox" class="name" name="name_{{$people->id}}" person-id="{{$people->id}}"value=""/>&nbsp;&nbsp;
        {{$people->name}}
    </td>
    <td>
        <input type="number" class="age" name="age_{{$people->id}}" person-id="{{$people->id}}" value="{{$people->age}}" disabled />
    </td>
    </tr>
 @endforeach

And then in the Javascript take one according to that attribute of the checkbox:

$(document).ready(function () {
    $('.name').on('change', function () {
        let id = $(this).attr('person-id')
        $('input.age[person-id="'+id+'"]').prop("disabled", !$(this).is(':checked'));
        $('input.age[person-id="'+id+'"]').val('');
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

Which is better in your opinion, using Dynamic approach or row identifiers approach ? Is there any specific reason to use which approach is better ?
with the dynamic you have to be sure that there will always be the same structure (which i guess you have). With the IDs - you are just exposing IDs in the html code, but its nothing tragical, both are totally fine to use. Just personal preference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.