0

I m trying to check all the checkboxes on the basis of their values but it only checked the first row. I don't know how to assign a value to the same id elements

HTML code:

<tbody class="col-lg-12 col-md-12" style="height: 400px; width: 400px; overflow: auto;">
                    @foreach (var item in Model.DetailList)
                    {
                        <tr>

                            <td>
                                @Html.DisplayFor(modelItem => item.FormCode, new { @id = "formCode" }) - @Html.DisplayFor(modelItem => item.FormName)
                            </td>

                            <td style="text-align:center;">

                                <input type="checkbox" id="view1" name="view" />
                            </td>
                            <td style="text-align:center;">
                                <input type="checkbox" id="add1" name="add" />
                            </td>
                            <td style="text-align:center;">
                                <input type="checkbox" id="edit1" name="edit" />
                            </td>
                            <td style="text-align:center;">
                                <input type="checkbox" id="delete1" name="delete1" />
                            </td>
                            <td style="text-align:center;">
                                <input type="checkbox" id="post1" name="post" />
                            </td>
                            <td style="text-align:center;">
                                <input type="checkbox" id="print1" name="print" />
                            </td>

                        </tr>
                    }
                </tbody>
Cleptus
  • 3,446
  • 4
  • 28
  • 34
Fahad GR
  • 1
  • 1
  • 2
    It is not recommended and also pointless. Have different IDs and loop on name or class. Also post rendered HTML and script in a [mcve] – mplungjan Dec 10 '18 at 10:53
  • Just loop on `document.querySelectorAll("tbody [type=checkbox]:checked")` – mplungjan Dec 10 '18 at 10:55

2 Answers2

0

Yes, you can but don't do that.

Element IDs should be unique, you can use other things like class on your elements to group them with something in common.

Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59
0

It's possible, but it ruins the point. An id is used to uniquely identify elements, use classes if you want to target multiple elements

Please read this Stackoverflow-post

Chris
  • 426
  • 2
  • 7
  • 18