In your code:
document.getElementsByClassName('abc');
Returns an array-like object of all child elements which have all of
the given class names.
MDN: getElementsByClassName
The result value is a HTMLCollection
.
In this scenario we can iterate over the collection.
With Javascript by Using HTMLCollection
:
<!DOCTYPE html>
<html>
<head>
<title>Onreset-3</title>
</head>
<body>
<form>
Username:
<input type="text" class="abc">
<br>
<br>Password:
<input type="password" class="abc">
<br>
<br>
</form>
<input type="button" onclick="reset()" value="Clear">
<script>
function reset() {
var a = document.getElementsByClassName('abc');
// a = HTMLCollection
console.log(a);
// You can iterate over HTMLCollection.
for (var i = 0; i < a.length; i++) {
// You can set the value in every item in the HTMLCollection.
a[i].value = "";
}
}
</script>
</body>
</html>
Without Javascript
However, your form can perfectly works with a reset button.
The <input type="reset" value="Clear">
must be inside the form tag.
Something like this:
<!DOCTYPE html>
<html>
<head>
<title>Onreset-3</title>
</head>
<body>
<form>
Username:
<input type="text" class="abc">
<br>
<br>Password:
<input type="password" class="abc">
<br>
<br>
<input type="reset" value="Clear">
</form>
</body>
</html>
Additional Information:
HTMLFormElement.reset()
: The HTMLFormElement.reset()
method restores a form element's default values. This method does the same
thing as clicking the form's reset button.
If a form control (such as a reset button) has a name or id of reset
it will mask the form's reset method. It does not reset other
attributes in the input, such as disabled.
Usage:
document.getElementById("myform").reset();
HTMLFormElement.reset()