console.clear()
function addItem(e) {
var list = document.getElementById("item").value;
var table = "<tr><td class='listedItem'>" + list + "</td><td><button class='buy'>Mark as bought</button></td>";
document.getElementById('thead').innerHTML = "<tr><td><b>Item description<b><td><b>Action</b></td></tr>";
document.getElementById('tbody').innerHTML += table;
}
$('#table').on('click', '.buy', function(e) {
$(e.target).closest('tr').find('.listedItem').addClass('bought');
//$(this).css("text-decoration", "line-through");
});
function sortAsc() {
var table, rows, switching, i, x, y, shouldSwitch;
table = document.getElementById("table");
switching = true;
while (switching) {
switching = false;
rows = table.rows;
for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName("td")[0];
y = rows[i + 1].getElementsByTagName("td")[0];
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
}
function sortDesc() {
var table, rows, switching, i, x, y, shouldSwitch;
table = document.getElementById("table");
switching = true;
while (switching) {
switching = false;
rows = table.rows;
for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName("td")[0];
y = rows[i + 1].getElementsByTagName("td")[0];
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
}
document.getElementById('addButton').addEventListener('click', addItem);
document.getElementById('sort_asc').addEventListener('click', sortAsc);
document.getElementById('sort_desc').addEventListener('click', sortDesc);
* {
box-sizing: border-box;
}
body {
background-color: #4abdac;
min-width: 250px;
}
h2 {
text-align: center;
font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
}
#container {
background-color: #FC4A1A;
max-width: 500px;
height: 100%;
margin: auto;
color: #f7b733;
border-radius: 25px;
padding: 15px;
}
#item {
margin: 0;
border: none;
border-radius: 0;
width: 75%;
padding: 10px;
float: left;
font-size: 16px;
}
#addButton,
#sort_asc,
#sort_desc {
padding: 10px;
width: 25%;
background: #d9d9d9;
color: #555;
float: left;
text-align: center;
font-size: 12px;
cursor: pointer;
transition: 0.3s;
border-radius: 0;
}
#sort_desc:hover {
background-color: #bbb;
}
#sort_asc:hover {
background-color: #bbb;
}
#addButton:hover {
background-color: #bbb;
}
td:hover {
background: #ddd;
transition: 500ms;
}
table {
clear: left;
word-break: break-all;
}
.bought {
text-decoration: line-through;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
<div id="shopping_list">
<h2>YOUR SHOPPING LIST</h2>
<form>
<input type="text" id="item">
<button id="addButton" type="button">Add item</button>
</form>
<button id="sort_asc">Sort Asc</button>
<button id="sort_desc">Sort Desc</button>
<table id="table">
<thead id="thead"></thead>
<tbody id="tbody"></tbody>
</table>
</div>
</div>