@@ -45,29 +45,36 @@ const deleteTask = (task) => {
4545 task . remove ( ) ;
4646} ;
4747
48- // add the task when click
49- taskAddBtn . addEventListener ( "click" , ( ) => {
48+ const addNewTask = ( ) => {
5049 // get input field value
5150 const newTask = taskInput . value ;
5251
53- if ( newTask !== '' && newTask . length >= 5 )
54- {
52+ if ( newTask !== '' && newTask . length >= 5 ) {
5553 // cloning the structure of tasks
5654 const newTaskField = taskField . cloneNode ( true ) ;
5755
5856 // select the task text field
5957 const newTaskText = newTaskField . querySelector ( ".taskText" ) ;
6058 newTaskText . textContent = newTask ;
6159
60+ // append the task and empty the inputBox value
6261 taskContainer . append ( newTaskField ) ;
63-
6462 taskInput . value = '' ;
6563 }
6664 else
6765 alert ( "Task must be of at least 5 characters to be registered." ) ;
6866
67+ // focus the input box for typing
6968 taskInput . focus ( ) ;
69+ }
70+
71+ // add the task when clicked plusIcon
72+ taskAddBtn . addEventListener ( "click" , addNewTask ) ;
7073
74+ // add the task when pressed Enter key
75+ taskInput . addEventListener ( "keydown" , ( e ) => {
76+ if ( e . key === "Enter" )
77+ addNewTask ( ) ;
7178} ) ;
7279
7380// check which task is clicked and which button is clicked
@@ -104,6 +111,7 @@ taskContainer.addEventListener("click", (e) => {
104111// remove all childs of "taskContainer" class
105112clearAllTaskBtn . addEventListener ( "click" , ( ) => {
106113
114+ // ask for confirmation
107115 if ( confirm ( "All the tasks will be cleared permanently." ) )
108116 {
109117 taskContainer . setHTML ( '' ) ;
0 commit comments