Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop though all input boxes and clear them

How can I, using javascript, loop through all input boxes on a page and clear the data inside of them (making them all blank)?

like image 760
Jason Kelly Avatar asked Nov 17 '12 22:11

Jason Kelly


1 Answers

Try this code:

​$('input').val('');

It's looping over all input elements and it's setting their value to the empty string. Here is a demo: http://jsfiddle.net/maqVn/1/

And of course if you don't have jQuery:

var inputs = document.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i += 1) {
    inputs[i].value = '';
}​​​​

Since I prefer the functional style, you can use:

Array.prototype.slice.call(
  document.getElementsByTagName('input'))
  .forEach(function (el) {
    el.value = '';
});
like image 107
Minko Gechev Avatar answered Oct 15 '22 04:10

Minko Gechev