vanilla-todo
vanilla-todo copied to clipboard
Empty field can be submitted
Solution might be: Adding .trim() after grabbing the value from that field
Hello tusharb,
Just simply add this code underneath first function addTodo(event) after event.preventDefault() function.
if (mainInput.value === ' ') { alert('You have to specify your task first.'); return false; }
This code will simply inform you, that the input value cannot be empty string or null value. It will return false, what means it will stop code from executing any further.
If you are working on the react project, in the Form components you can change the submitTodoHandler as follows
const submitTodoHandler = (e) => { e.preventDefault(); console.log(inputText); if(!(inputText === "")) { setTodos([ ...todos, {text: inputText, completed: false, id: Math.random()*1000 }, ]); setInputText(""); }else{ alert("Kindly specify your task first."); } };