[Solved] i need to create and remove html elements [closed]


You have to wrap the argument in quotes since it is a string.

newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\"#\" onclick="removeElement(\''+divIdName+'\')">Remove the div  '+divIdName+' </a>';

Below is the working snippet

function addElement() {

  var ni = document.getElementById('myDiv');

  var numi = document.getElementById('theValue');

  var num = (document.getElementById('theValue').value -1)+ 2;

      numi.value = num;

  var newdiv = document.createElement('div');

  var divIdName="my"+num+'Div';

  newdiv.setAttribute('id',divIdName);

  newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\"#\" onclick="removeElement(\''+divIdName+'\')">Remove the div  '+divIdName+' </a>';

  ni.appendChild(newdiv);

}
function removeElement(divNum) {

  var d = document.getElementById('myDiv');

  var olddiv = document.getElementById(divNum);

  d.removeChild(olddiv);

}
<input type="hidden" value="0" id="theValue" />

<p><a href="https://stackoverflow.com/questions/35582541/javascript:;" onclick="addElement();">Add Some Elements</a></p>

<div id="myDiv"> </div>

1

solved i need to create and remove html elements [closed]