I have a form and I’m trying to have a button to zero out all the fields. Can’t use a ‘reset’ type button, as there are proper default values there, populated externally.
It’s incredibly basic, and I’ve stripped out any complication I can think of, but it isn’t working. It’s generated by a jinja template. When I click, however, absolutely nothing happens. No feedback in the logs, no change in the input field numbers. What amI missing?
<html>
<head>
...
<script>
function zero_out() {
document.getElementsByClassName("itemCount").value=0;
}
</script>
</head>
<body>
<form method="post">
<label for="thing">thing</label>
<input type="number" pattern="d*"
name="thing" style="width: 40px;"
class="itemCount"
placeholder="0"
value=""></input>
<br />
...
<button onclick="zero_out()" type="button" class="button">ZERO</button>
</form>
2
Answers
If you’re only resetting one input, just use
document.querySelector
to get the element. (document.getElementsByClassName
returns aHTMLCollection
of all elements with the specified class.)Just reset it with looping for collections of
getElementsByClassName
like this:Full code: