I am creating a web app where I allow the user to manipulate some data held in Javascript array. Its easy enough to generate a series of divs or an HTML list to represent the array (simply iterating through and adding the HTML) but I also want each array element to have a ‘delete’ button in the HTML that removes the corresponding Javascript array element and then update the HTML list.
To clarify the list would look something like this to the user:
Item 1 (Delete)
Item 2 (Delete)
Ideally I would like the ‘Delete’ link to, when clicked, call a Javascript method with the item index (‘remove_item(index)’ for example).
I have thought of a couple of awkward ways of doing this but I feel this is something that must have been done before and wondered if anyone had a sturdy, fast solution to this?
Grab the inner HTML of the sibling of the delete button, then look for that value in the array. Then remove it, and remove the parent of the delete button from the DOM.
http://api.jquery.com/siblings/
http://api.jquery.com/parent/