Question

I have a script like this. How do I continue to add functions : remove items?

JS

counter = 0;
function action() {
  counterNext = counter + 1;
  document.getElementById("input"+counter).innerHTML = "<p><input type='text' name='data[]' class='name'>&nbsp;&nbsp;<a href='#' class='remove'><img src='remove.png' width='20' height='20' border='0' id='remove'/></a></p><div id=\"input"+counterNext+"\"></div>";
  counter++;
}

HTML

<p>
  <input type='text' name='data[]' class='name'>
  <a href="javascript:action();">Add</a>
</p>
<div id="input0"></div>

Can someone help me...

Was it helpful?

Solution

In order to remove items change the following code:

<a href='#' class='remove'>

to:

<a href='#' onclick='this.parentElement.parentElement.removeChild(this.parentElement); return false;' class='remove'>

Example: http://jsfiddle.net/uRLFR/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top