0

I have the follwoing:

HB.formButtons.deactivatingButton = function(inputs) {
    inputs.forEach(function(argument){
        argument.parent().removeClass(HB.formButtons.SUBMIT_BUTTON_STYLE);
    });
}

which I than call in various parts like so:

HB.formButtons.deactivatingButton($(HB.personalDetails.SUBMIT_PERSONAL_DETAILS_SELECTOR), $(HB.personalDetails.CANCEL_PERSONAL_DETAILS_SELECTOR));

The console. throws the following error:

Uncaught TypeError: formsInput.forEach is not a function

Why?

Aessandro
  • 5,517
  • 21
  • 66
  • 139
  • 1
    maybe because `inputs` is a jQuery object and not an Array ? (you are passing multiple parameters to `deactivatingButton ` , not an array) – Hacketo Oct 08 '15 at 14:02
  • http://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript – Mihai Matei Oct 08 '15 at 14:03

2 Answers2

1

Considering your input to be HTMLCollection, the forEach doesn't work on a collection. forEach works on Array

You can, however, make it work like

HB.formButtons.deactivatingButton = function(inputs) {
    [].forEach.call(inputs, function(argument){
        argument.parent().removeClass(HB.formButtons.SUBMIT_BUTTON_STYLE);
    });
}
AdityaParab
  • 7,024
  • 3
  • 27
  • 40
1

If you wanted to pass your function array of two elements you have to use square brackets [].

HB.formButtons.deactivatingButton(
[
    $(HB.personalDetails.SUBMIT_PERSONAL_DETAILS_SELECTOR),
    $(HB.personalDetails.CANCEL_PERSONAL_DETAILS_SELECTOR)
]);

alternative you can use the special variable arguments in your function. Described at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

exyi
  • 452
  • 3
  • 12