Change innerHtml of div according to value of model item in view .NET6 EF6 Razor Pages No MVC
I am trying to have a due date optional in a todo list. Since date fields are not nullable/required, I set the default due date to 09/09/9999. That date represents a todo that does not have a due date. What I want is to have the innerHtml of that due date display “None” if the due date is 09/09/9999.
My code however shows “None” in the due date div in the table of only the first row, regardless of the date.
Here is the javascript code
JavaScript
x
10
10
1
function hideDueDate () {
2
const dueDateDiv = document.querySelector('#due-date');
3
4
if (dueDateDiv.innerHTML = "09/09/9999") {
5
dueDateDiv.innerHTML = "None";
6
}
7
}
8
9
hideDueDate();
10
And here is the view.
JavaScript
1
33
33
1
<tbody>
2
@*READ*@
3
@foreach(var obj in Model.ToDo)
4
{
5
<tr>
6
<td width="10%">@Convert.ToString(string.Format("{0:MM/dd/yyyy}", obj.CreateDate))</td>
7
<td width="20%">@obj.Name</td>
8
<td width="40%">@obj.Description</td>
9
<td width="10%">
10
<div id="due-date">@Convert.ToString(string.Format("{0:MM/dd/yyyy}", obj.DueDate))</div>
11
</td>
12
<td width="20%" class="">
13
<div class="row d-flex justify-content-center">
14
<input asp-for="@obj.Complete" class="form-check-input mt-3" type="checkbox" readonly="readonly" />
15
</div>
16
</td>
17
<td>
18
<div class="w-65 btn-group" role="group">
19
<a asp-page="View" asp-route-id="@obj.Id" class="btn btn-outline-info rounded-pill mx-2">
20
<i class="bi bi-binoculars"> View</i>
21
</a>
22
<a asp-page="Edit" asp-route-id="@obj.Id" class="btn btn-outline-success rounded-pill mx-2">
23
<i class="bi bi-pencil-square"> Edit</i>
24
</a>
25
<a asp-page="Delete" asp-route-id="@obj.Id" class="btn btn-outline-danger rounded-pill mx-2">
26
<i class="bi bi-trash-fill"> Delete</i>
27
</a>
28
</div>
29
</td>
30
</tr>
31
}
32
</tbody>
33
Advertisement
Answer
1.document.querySelector('#due-date')
can only get the first matched element. You need use .each()
.
2.The condition should be dueDateDiv.innerHTML == "09/09/9999"
.
Change your code like below:
JavaScript
1
8
1
function hideDueDate () {
2
$('div #due-date').each(function() {
3
if(this.innerHTML=="09/09/9999"){
4
this.innerHTML= "None";
5
}
6
});
7
}
8