Below is the code snippet of what is happening. If you type task into the input then just add it three times you can see that when you click on the items added below only the first and third will get the strikethrough. A similar problem would happen with four or five items added.
// View Controller
let UICtrl = (function () {
let DOMstrings = {
inputBtn: '#input-btn',
toDo: '#text-input',
listContainer: '#to-do-list'
};
return {
getInput: function() {
return {
item: document.querySelector(DOMstrings.toDo).value
};
},
addListItem: function(obj) {
let html, newHtml, element;
// Create HTML string with placeholder text
element = DOMstrings.listContainer;
if(obj.toDo) {}
else {
return
};
html = '<li name= "list-item-task" id= "list-item-%id%" class="list-item-task">%toDo%<ion-icon id="icon-remove" name="close-circle-outline" class="icon-remove animate__animated"></ion-icon></li>';
newHtml = html.replace('%id%', obj.id);
newHtml = newHtml.replace('%toDo%', obj.toDo);
newHtml = newHtml.replace('%date%', obj.date);
// Insert the HTML into the DOM
document.querySelector(element).insertAdjacentHTML('beforeend', newHtml);
},
jQuery: (function() {
//click list items to add strikethrough
$(".list-item-task").click(function () {
if ($(this).hasClass("task-complete")) {
$(this).removeClass("task-complete")
} else {
$(this).addClass("task-complete")
}
});
}),
getDOMstrings: function() {
return DOMstrings;
}
};
})();
let listCtrl = (function (){
let Item = function(id, toDo, date) {
this.id = id;
this.toDo = toDo;
this.date = date;
};
let toDoList = [];
return {
addItem: function(toDo, date) {
let newItem, ID;
// Create new ID
if (toDoList.length > 0) {
ID = toDoList.length;
} else {
ID = 0;
}
newItem = new Item(ID, toDo, date)
toDoList.push(newItem);
return newItem;
},
}
})();
let controller = (function (UICtrl, listCtrl) {
let DOM = UICtrl.getDOMstrings();
let setupEventListeners = function() {
document.querySelector(DOM.inputBtn).addEventListener('click', ctrlAddItem);
};
let ctrlAddItem = function () {
let input, newItem;
input = UICtrl.getInput();
newItem = listCtrl.addItem(input.item, input.date);
UICtrl.addListItem(newItem);
UICtrl.jQuery();
};
return {
init: function() {
setupEventListeners();
}
};
})(UICtrl, listCtrl);
controller.init();
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/*----------------------------------------------*/
/*HEADER*/
/*----------------------------------------------*/
html,
body {
background-color: #fff;
color: #000;
font-family: 'Lato', sans-serif;
font-size: 20px;
font-weight: 300;
text-rendering: optimizeLegibility;
overflow-x: hidden;
}
.row {
background-color: rgba(255, 255, 255, .55);
width: 100%;
}
.main-nav li {
color: #000;
font-family: 'Lato', sans-serif;
font-size: 20px;
font-weight: 400;
display: inline-block;
border: none;
width: 20%;
}
.main-nav li:hover {
border-bottom: 2px solid #e67e22;
background: transparent;
}
.input-container {
text-align: center;
display: block;
padding-top: 20px;
}
.input-container {
background-color: rgb(248, 248, 248);
text-align: center;
height: 80px;
float: inline-block;
}
.text-input {
width: 37.5%;
height: 50%;
font-size: 18px;
font-family: 'Lato', sans-serif;
border: 1px solid #b9b9b9;
border-radius: 5px;
}
.date-input {
width: 15%;
height: 50%;
font-size: 18px;
font-family: 'Lato', sans-serif;
border: 1px solid #b9b9b9;
border-radius: 5px;
}
.btn-add-item {
height: 40px;
width: 6.25%;
font-size: 18px;
font-family: 'Lato', sans-serif;
color: #ffb325;
background-color: transparent;
border: 2px solid #ffb325;
border-radius: 5px;
}
.list-container {
position: relative;
left: 25%;
margin-top: 10px;
}
.list-item-task {
border-radius: 5px;
}
li {
text-align: center;
width: 50%;
display: block;
border: 1px solid #b9b9b9;
padding: 10px 30px 10px 30px;
}
li:hover {
background-color: #ececec;
}
.task-complete {
text-decoration: line-through;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header>
</header>
<div class="input-container">
<input type="text" id="text-input" class="text-input" placeholder=" Task" required>
<input type="submit" class="btn-add-item icon" id='input-btn' value='Add Item'></input>
</div>
<div class="list-container">
<ul id='to-do-list'>
</ul>
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="script.js"></script>
</html>
The jQuery code I have is only being applied to every other list item in the HTML. When in the dev tools I can see it register the click event listener, but it does not add the class when needed for some of the items. It will successfully add it to the last item added in a list, but not the item before that.
I realize the issue may lie somewhere else within the code and would appreciate thoughts on what could be causing this.
$(".list-item-task").click(function () {
if ($(this).hasClass("task-complete")) {
$(this).removeClass("task-complete")
} else {
$(this).addClass("task-complete")
}
});
Here is an image showing the issue
Thanks!