jeudi 22 décembre 2022

how to set checkbox checked attribute in ejs

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"
  }



Aucun commentaire:

Enregistrer un commentaire