I have created a button 'Add Goal'. On clicking it A table would be created along with a button named 'Remove Goal'. On clicking the 'Remove Goal' button, that specific table should be deleted. But instead the lowest table gets deleted. What code should I write in the 'removeGoal()' function so as to delete that specific table the 'Remove Goal' button is associated with? Below is the following HTML and Javascript Code.
HTML CONTENT:
<!DOCTYPE html>
<html lang="en-US">
<head>
</head>
<body>
<button type="button" onclick="addGoal()">Add Goal</button>
</body>
</html>
JAVASCRIPT CONTENT:
<script language="javascript" type="text/javascript">
function addGoal() {
var x = document.createElement("table");
x.id = "table1";
document.body.appendChild(x);
var y = document.createElement("tr");
x.appendChild(y);
var z = document.createElement("th");
z.innerHTML = "Goal Name";
y.appendChild(z);
var a = document.createElement("tr");
x.appendChild(a);
var b = document.createElement("td");
a.appendChild(b);
var c = document.createElement("input");
c.type = "text";
c.name = "goalName";
b.appendChild(c);
var d = document.createElement("button");
d.type = "button";
d.innerHTML = "Remove Goal";
d.id = "button1";
d.setAttribute("onclick", "removeGoal()");
document.body.appendChild(d);
}
function removeGoal() {
var removeTab = document.getElementById('table1');
var parentE1 = removeTab.parentElement;
parentE1.removeChild(removeTab);
var removeBut = document.getElementById('button1');
var parentE2 = removeBut.parentElement;
parentE2.removeChild(removeBut);
//This removes the lowest table.
//Syntax to delete the table the 'Remove Goal' button is associated with
}
<script>