1

I have a HTML table in my MVC View.

Here is the code of this table:

<table class="table" >
    @{int rowNo = 0;}
    <tr>
        <td data-toggle="modal" data-target="#newAppointment" style="height: 40px; width: 40px; background: red;">
            <img style="object-fit: cover;" src='@Url.Content("~/Images/plus.png")'/>
        </td>
    </tr>
    <tr style="background: white">
        <th></th>
        <th style="font-size: 20px; text-align: center;color:#1d69b4;">
            Date of birth
        </th>
        <th style="font-size: 20px; text-align: center;color:#1d69b4;">
            Last Name
        </th>
        <th style="font-size: 20px; text-align: center;color:#1d69b4;">
            First Name
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr id="patients">
            <td class="point">
                @(rowNo += 1)
            </td>
            <td class="title">
                @Html.DisplayFor(modelItem => item.Date_of_Birthday)
            </td>
            <td class="title">
                @Html.DisplayFor(modelItem => item.Last_name)
            </td>
            <td class="title">
                @Html.DisplayFor(modelItem => item.First_name)
            </td>
        </tr>
    }
</table>

I need to delete all the rows in the table, so I need to delete everything in patients.

I tried to do it like the following via JS, but it only deletes the first row:

<script>
    $('#search').click(function () {
        $("#patients").remove();
    });
</script>

How can I delete all the rows?

2
  • 1
    The element with id=patients is a <tr> element and its invalid html because of the duplicate id attributes. Add a <thead> and <tbody> element with an id attribute and remove the <tr> elements from the <tbody> using .empty() Commented Jul 23, 2017 at 11:34
  • Thank's , it helps@StephenMuecke Commented Jul 23, 2017 at 11:39

1 Answer 1

1

An id is used to identify a unique element in the DOM. You're incorrectly generating multiple elements with the same id within your foreach loop. You might consider changing from id to class. Which allows the identification of multiple elements of the same type:

@foreach (var item in Model) {
    <tr class="patients">
        <!-- your code goes here -->
    </tr>
}

Then the appropriate selector removes all rows:

$('#search').click(function () {
   $(".patients").remove();
});
Sign up to request clarification or add additional context in comments.

Comments

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.