@@ -45,29 +45,36 @@ const deleteTask = (task) => {
45
45
task . remove ( ) ;
46
46
} ;
47
47
48
- // add the task when click
49
- taskAddBtn . addEventListener ( "click" , ( ) => {
48
+ const addNewTask = ( ) => {
50
49
// get input field value
51
50
const newTask = taskInput . value ;
52
51
53
- if ( newTask !== '' && newTask . length >= 5 )
54
- {
52
+ if ( newTask !== '' && newTask . length >= 5 ) {
55
53
// cloning the structure of tasks
56
54
const newTaskField = taskField . cloneNode ( true ) ;
57
55
58
56
// select the task text field
59
57
const newTaskText = newTaskField . querySelector ( ".taskText" ) ;
60
58
newTaskText . textContent = newTask ;
61
59
60
+ // append the task and empty the inputBox value
62
61
taskContainer . append ( newTaskField ) ;
63
-
64
62
taskInput . value = '' ;
65
63
}
66
64
else
67
65
alert ( "Task must be of at least 5 characters to be registered." ) ;
68
66
67
+ // focus the input box for typing
69
68
taskInput . focus ( ) ;
69
+ }
70
+
71
+ // add the task when clicked plusIcon
72
+ taskAddBtn . addEventListener ( "click" , addNewTask ) ;
70
73
74
+ // add the task when pressed Enter key
75
+ taskInput . addEventListener ( "keydown" , ( e ) => {
76
+ if ( e . key === "Enter" )
77
+ addNewTask ( ) ;
71
78
} ) ;
72
79
73
80
// check which task is clicked and which button is clicked
@@ -104,6 +111,7 @@ taskContainer.addEventListener("click", (e) => {
104
111
// remove all childs of "taskContainer" class
105
112
clearAllTaskBtn . addEventListener ( "click" , ( ) => {
106
113
114
+ // ask for confirmation
107
115
if ( confirm ( "All the tasks will be cleared permanently." ) )
108
116
{
109
117
taskContainer . setHTML ( '' ) ;
0 commit comments