I am new to javascript, need some help.
I am working on the Add/Remove
feature to add and delete data from the table up on input. Add operation is working fine but I am not able to delete
the row in the table.
I have gone through some of the doc and links but I am not getting.
Can someone help me with it?
Code Snippet:
<!DOCTYPE html>
<html>
<head lang="en">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<style>
body {
margin: 85px;
}
.btn-primary {
cursor: pointer;
}
</style>
</head>
<body>
<form class="container">
<div class="form-group">
<label for="name">Enter Name</label>
<input type="text" class="form-control" id="firstname" placeholder="Enter Your Name">
</div>
<div class="form-group">
<label for="age">Enter Age</label>
<input type="text" class="form-control" id="age" placeholder="Age">
</div>
<div class="form-group">
<label for="subject">Enter Subject</label>
<input type="text" class="form-control" id="stusubject" placeholder="Subject">
</div>
<input type="button" onclick="showName()" class="btn btn-primary" value="submit">
</form>
<table class="table container mt-5" id="studList">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Age</th>
<th scope="col">Subject</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<tr>
</tr>
</tbody>
</table>
</body>
</html>
<script>
var student = [{
nname: "abc",
age: 18,
subject: "eng"
},
{
nname: "red",
age: 20,
subject: "math"
}
]
function showName() {
var firstName = document.getElementById("firstname").value;
var age = document.getElementById("age").value;
var stusubject = document.getElementById("stusubject").value;
console.log("firstName :", firstName);
console.log("Age :", age);
console.log("subject :", stusubject);
// push the data in array
var newstud = {
nname: firstName,
age: age,
subject: stusubject,
}
student.push(newstud);
//console.log(newstud);
console.log(student);
var table = ' <tr>' +
'<td>' + firstName + '</td>' +
'<td>' + age + '</td>' +
'<td>' + stusubject + '</td>' +
'<td> <input type="button" onclick="delName(r)" class="btn btn-primary" value="Delete"></td></td>' +
'</tr>'
$(table).appendTo('#studList tbody');
console.log("table :", table);
}
//delete the row onclcik
function delName(r) {
var i = r.parentNode.parentNode.rowIndex;
document.getElementById("studList").deleteRow(i);
}
var table = ' <tr>' +
'<td>' + firstName + '</td>' -
+'<td>' + age + '</td>' +
'<td>' + stusubject + '</td>' +
'</tr>'
$(table).deleteRow('#studList tbody');
console.log("table :", table);
</script>