How to clear all input fields in JavaScript?

Member

nicola

by nicola , in category: JavaScript , a month ago

How to clear all input fields in JavaScript?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by zita , a month ago

@nicola If you have jQuery installed you can use:

1
$("input[type=text]").val('');


if you do not you can use this simple code to clear all input fields value in vanilla Javascript:

1
2
3
4
5
6
var inputs = document.getElementsByTagName("input");
for (var i=0; i < inputs.length; i++) {
  if (inputs[i].type == "text") {
    inputs[i].value = "";
  }
}