Bala Vignesh C S Bala Vignesh C S - 3 months ago 7
HTML Question

I'm trying to add textboxes within ordered list onclick of a button using JavaScript and also increment ID name while adding the textbox

I tried using document.createElement, which works fine to add textbox but I can't increment ID name.
I am using it in a metro app.

Answer
<form>
  No. of lines:
  <input type="text" id="number" value="0" width=20px/>
  <br/>
  <input type="button" onclick="incrementValue()" value="Add Line" />
</form>
<div>
  <ol id="linedata">
  </ol>
</div>

function incrementValue() {
  var value = parseInt(document.getElementById('number').value, 10);
  value = isNaN(value) ? 0 : value;
  value++;
  document.getElementById('number').value = value;
  var list = document.createElement('li');
  var box = document.createElement('input');
  box.type = 'text';
  box.id = 'line' + value;
  box.placeholder='Line ' + value;
  list.appendChild(box);
  document.getElementById("linedata").appendChild(list);
}
Comments