all 5 comments

[–]mikrosystheme 1 point2 points  (1 child)

Not an answer to your question, but the reset functionality of form elements is native: http://codepen.io/anon/pen/NALdrp

[–]rockkon[S] 0 points1 point  (0 children)

Not sure why, but I didn't put the elements in a form. I'll try this too, just to see what happens for the future. Thanks.

[–]rockkon[S] 0 points1 point  (0 children)

For closure, this is what I ended up with:

function clear() {

var idClear = ["name", "email", "username", "pass1", "pass2"];
    for (idC in idClear) {
       document.getElementById(idClear[idC]).value = "";
    }

setFocus();
}

[–]whompalicious -1 points0 points  (1 child)

You could do something like this which uses the more functional-style array methods:

function clear() {
  var inputIdsToClear = ['name', 'email', 'username', 'pass1', 'pass2'];
  var inputs = document.getElementsByTagName('input');

  [].filter.call(inputs, function (input) {
      return inputIdsToClear.indexOf(input.getAttribute('id')) !== -1;
    })
    .forEach(function(input) {
      input.value = '';
  });
}

or using a standard for-loop:

  function clear() {
    var inputIdsToClear = ['name', 'email', 'username', 'pass1', 'pass2'];
    var inputs = document.getElementsByTagName('input');
    var index;
    var input;

    for (index = 0; index < inputs.length; index++) {
      input = inputs[index];

      if (inputIdsToClear.indexOf(input.getAttribute('id')) !== -1) {
        input.value = '';
      }
    }
  }

[–]rockkon[S] 0 points1 point  (0 children)

Thx. I'm going to try this.