the7k the7k - 1 month ago 15
Javascript Question

Javascript not incrementing a variable

Please note that I have read previous and related questions to this on this website, and I didn't have luck finding what's wrong.

I would like to keep count every time the user clicks a the

add row
button. Here's the code I have that's not working.



function add_more_row() {
var rows_count = ParseInt(document.getElementById("rows_count").value);
rows_count += 1;
}

<input type="text" value="0" id="rows_count" />
<input onclick="add_more_row();" type="button" value="add row" />





What am I doing wrong?

Answer

Your code only gets the value and increases it, does not assign the value to the input field. Add this line after the increment statement:

document.getElementById("rows_count").value = rows_count;

Also it's parseInt() with lowercase p not ParseInt().

function add_more_row() {
  var rows_count = parseInt(document.getElementById("rows_count").value);
  rows_count += 1;
  document.getElementById("rows_count").value = rows_count;
}
<input type="text" value="0" id="rows_count" />
<input onclick="add_more_row();" type="button" value="add row" />