In ejs file i cannot get the checkbox's value when i use checked attribute. If i remove checked i can get the value.
<% newListItems.forEach(function(item) { %>
<form action="/delete" method="post">
<div class="item">
<input type="checkbox" checked value="<%=item._id%>" name="modified" onchange="this.form.submit()" >
<p><%= item.name %></p>
</div>
</form>
<% }) %>
"dependencies": {
"body-parser": "^1.18.3",
"ejs": "^3.1.8",
"express": "^4.16.3",
"mongoose": "^6.8.1"
}
I first asked my question as follows, because I thought that the error was how I set a boolean attribute, but actually the error was the boolean attribute itself. For this reason, I changed my question as above. But since helpful friends answered my first question, I didn't want to delete the first version of the question.
Initial version of my question:
I want to set checked attribute of checkbox conditionally. When i don't use checked attribute, i can get the value attribute in the post method correctly. However when i add checked attribute to checkbox, the code sets the checked property but I cannot get the value attribute of the checkbox (it becomes undefined in post method). Please can someone explain to me.
When i use completed variable as checked attribute, i cannot get the value of checkbox.
<% newListItems.forEach(function(item) { %>
<form action="/delete" method="post">
<div class="item">
<% let completed = item.done ? "checked" : "" %>
<input type="checkbox" value="<%=item._id%>" <%=completed%> name="modified" onchange="this.form.submit()">
<p><%= item.name %></p>
</div>
</form>
<% }) %>
"dependencies": {
"body-parser": "^1.18.3",
"ejs": "^3.1.8",
"express": "^4.16.3",
"mongoose": "^6.8.1"
}
You can do something like this