Patrick Lemark
Patrick Lemark

Reputation: 43

Javascript to hide value if it's zero

I have a form who gives data from database I have number type inputs. I want to hide and leave empty the input that displays "0", without replace it in database after submit.

I found how to do it with table cells. But not with inputs.

 <script type="text/javascript">
var table = document.getElementById("myTable");
var cells = table.getElementsByTagName("td");
for (var i = 0; i < cells.length; i++) {
  if (parseInt(cells[i].textContent, 10) === 0) {
	cells[i].innerHTML = ' ';

  }
}
</script>

Thx.

Upvotes: 0

Views: 1759

Answers (1)

KAD
KAD

Reputation: 11102

Filter input selection by value where it equals 0 with filter() function and then remove these elements from DOM not be submitted with the form with remove() function

   $('input').filter(function() { return $(this).val() === 0; }).remove();

Upvotes: 1

Related Questions