<!DOCTYPE html>
<html>
<head>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<style>
body {
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f5f5f5;
}
.wrapper {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.table-wrapper {
/* width: 90vw;
max-width: 900px;
min-height: 80px; */
width: 600px;
background-color: white;
border: 1px solid #e6edf0;
max-height: 400px;
overflow-y: auto;
}
.table-wrapper>.row {
height: 40px;
width: 100%;
border-bottom: 1px solid #ecf1f4
}
.table-wrapper>.row.header {
background-color: #ecf1f4;
font-weight: lighter;
color: #b0b0b0;
text-transform: uppercase;
}
.table-wrapper>.row>span {
display: inline-block;
line-height: 40px;
text-align: center;
font-size: 12px;
position: relative;
}
.table-wrapper>.row>span:nth-child(1) {
width: 40px;
}
.table-wrapper>.row>span:nth-child(2) {
width: 80px;
}
.table-wrapper>.row>span:nth-child(3) {
width: 60px;
}
.table-wrapper>.row>span:nth-child(4) {
width: 60px;
}
.table-wrapper>.row>span:nth-child(5) {
width: 80px;
}
.table-wrapper>.row>span:nth-child(6) {
width: 40px;
color: #c0c0c0;
}
.table-wrapper input[type=text],
.table-wrapper select {
height: 30px;
box-sizing: border-box;
max-width: 50px;
vertical-align: middle;
}
i.fa-trash {
cursor: pointer;
font-size: 16px;
}
.btn-wrapper {
width: 100%;
height: 40px;
background-color: white;
margin-top: 15px;
border: 1px solid #e6edf0;
box-sizing: border-box;
line-height: 40px;
text-align: center;
font-size: 13px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="wrapper">
<div id="table" class="table-wrapper">
<div class="row headed">
<span class=" ">idx</span>
<span class=" ">name</span>
<span class=" ">type</span>
<span class=" ">options</span>
<span class=" ">output</span>
<span class=" "> </span>
</div>
<div class="row">
<span class=" ">1</span>
<span class=" "><input type="text" /></span>
<span class=" "><select><option>Name</option></select></span>
<span class=" "><select><option>Korean</option><option>English + Korean</option></select></span>
<span class=" ">Kim</span>
<span class="btn deleteButton"><i class="fa fa-trash" aria-hidden="true"></i></span>
</div>
</div>
<div id="addButton" class="btn btn-primary .btn-wrapper">
<i class="fa fa-plus" aria-hidden="true"></i> ADD ROW
</div>
</div>
<script>
var add = document.getElementById('addButton');
var table = document.getElementById('table');
add.addEventListener('click', function(e) {
/* insertAdjacentHTML() is like innerHTML on steroids
|| See references for details
*/
table.insertAdjacentHTML('beforeend', `<div class="row">
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span class="btn deleteButton">
<i class="fa fa-trash" aria-hidden="true"></i>
</span>
</div>`);
});
/* Register table to click event...
== if e.target (the element clicked i.e. .fa-trash)
|| is NOT e.currentTarget
|| (the element registered to event i.e. #table)...
== if e.target has the class .fa-trash...
== Get the grandparent of e.target (i.e. .row)
== Remove the .row
*/
table.addEventListener('click', function(e) {
if (e.target !== e.currentTarget) {
if (e.target.classList.contains('fa-trash')) {
var row = e.target.parentElement.parentElement;
table.removeChild(row);
}
e.stopPropagation();
}
}, false);
</script>
</body>
</html>