skip to Main Content

The ‘checked’ value on my list is not changing, i don’t know why that is

I’m making a toDo app

My State Values:

 const [newItem, setNewItem] = useState("");
 const [toDos, setToDos] = useState([]);

My function

  function toggleToDo(id, completed) {
    setToDos((currentTodo) => {
      return currentTodo.map((todo) => {
        if (todo.id === id) {
          return { ...todo, completed};
        }
        return todo;
      });
    });
  }

My List

<ul className="list">
        {toDos.map((toDo) => {
          const { id, title, completed } = toDo;
          return (
            <li key={id}>
              <label htmlFor="">
                <input
                  type="checkbox"
                  checked={completed}
                  onChange={(e) => toggleToDo(id, e.target.checked)}
                />
                {title}
              </label>
              <button className="btn btn-danger">Delete</button>
            </li>
          );
        })}
      </ul>

i am expecting the ‘e.target.checked’ to change when the function toggleToDo is called

2

Answers


  1. Chosen as BEST ANSWER

    My label <label htmlFor=""> was not linked thus my functionality was not working


  2. The checked attribute does not indicate whether this checkbox is currently checked: if the checkbox’s state is changed, this content attribute does not reflect the change.

    The value attribute is the one that changes when the checkbox state changes.

    (see: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input/checkbox#additional_attributes)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search