So I want to create a function that will let me add/remove an element in the HTML, I’m already done with the “add” part that increments the id value onchange (example: id=tag1, id=tag2, etc). My problem is on the “remove” part, I don’t know how to put an incremental value inside onclick=remove_tag(). Here’s my code
JavaScript
x
12
12
1
function update() {
2
var selObj = document.getElementById("skill_tags");
3
var selVal = selObj.options[selObj.selectedIndex].text;
4
let counter = 0;
5
document.getElementById("textarea").innerHTML += "<div class='tags_inline' id='tag'><li class='list-inline-item'><span class='badge badge-dark'>" + selVal + "<button class='fa fa-times-circle text-white' id='delete' onclick=remove_tag('tag"+ counter +"');></button></span></li></div>";
6
$("#textarea div").each(function(i){this.id = "tag" + (i + 1)})
7
}
8
9
function remove_tag(id) {
10
document.getElementById(id).innerHTML = "";
11
}
12
What I want to do is to make my onclick on the button to be (onclick=”remove_tag1″, onclick=”remove_tag2″, onclick=”remove_tag3″, etc). Sorry for the question, still a newbie in JavaScript. Thanks for the help. Here’s an image https://pasteboard.co/k7hb7cVHSQHj.png
JavaScript
1
20
20
1
<div class="resume-skill-item">
2
<h5>
3
<ul class="list-inline">
4
<div align="right">
5
<select id="skill_tags" onchange="update()">
6
<option selected="true" disabled="disabled">*Select All That Applies</option>
7
<option value="mechanic">Mechanic</option>
8
<option value="appliance_repairer">Appliance Repairer</option>
9
<option value="carpenter">Carpenter</option>
10
<option value="plumber">Plumber</option>
11
<option value="technician">Technician</option>
12
</select>
13
</div>
14
</ul>
15
<div id="textarea" class="large-single-textarea">
16
</div>
17
</h5>
18
</div>
19
```
20
Advertisement
Answer
You can use data attribute on delete button to keep reference on added items when you want to delete them.
JavaScript
1
11
11
1
function update(e) {
2
var selObj = document.getElementById("skill_tags");
3
var selVal = selObj.options[selObj.selectedIndex].text;
4
let counter = 0;
5
document.getElementById("textarea").innerHTML +=
6
`<div class="tags_inline" id="${e.value}"><li class="list-inline-item"><span class="badge badge-dark">"${selVal}"<button data-select-id="${e.value}" class="fa fa-times-circle text-white" id="delete" onclick=remove_tag(this) >remove</button></span></li></div>`;
7
}
8
9
function remove_tag(e) {
10
document.getElementById(e.dataset["selectId"]).remove();
11
}
JavaScript
1
19
19
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<div class="resume-skill-item">
3
<h5>
4
<ul class="list-inline">
5
<div align="right">
6
<select id="skill_tags" onchange="update(this)">
7
<option selected="true" disabled="disabled">*Select All That Applies</option>
8
<option value="mechanic">Mechanic</option>
9
<option value="appliance_repairer">Appliance Repairer</option>
10
<option value="carpenter">Carpenter</option>
11
<option value="plumber">Plumber</option>
12
<option value="technician">Technician</option>
13
</select>
14
</div>
15
</ul>
16
<div id="textarea" class="large-single-textarea">
17
</div>
18
</h5>
19
</div>