Skip to content
Advertisement

How to remove an incremental element in HTML using JavaScript

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

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

Advertisement

Answer

You can use data attribute on delete button to keep reference on added items when you want to delete them.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement