1

I've managed to get the loop working in all browsers apart from Internet Explorer (which doesn't seem to support forEach).

The JavaScript cpde:

function validate() {
    var msg = '';
    var i = 0;

    arr.forEach(
        function validateinfo(){
            if (getRBtnName('yesNo_' + i + '_0' == "" && 'yesNo_' + i + '_0') == "") {
                msg = 'Please select yes/no for all users'
            }
            if (msg == '') {
                return true;
            }
            is++;
        }
    )

    if (msg == '') {
        reloadpage();
    }

    if (msg != '') {
        alert(msg);
        return false;
    }
}


function reloadpage(){
    window.location.reload()
}

The array is being set in the PHP file rather than passed in. It's being set using:

<script type="text/javascript">
    var arr = <?php echo json_encode($arr) ?>;
</script>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 3
    Have a look here http://stackoverflow.com/questions/9329446/for-each-in-an-array-how-to-do-that-in-javascript – Pedro Monte Jul 23 '13 at 10:10
  • [Array.forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) – Andreas Jul 23 '13 at 10:10
  • Just noticed the increment says is++ in the code above, but the actual code does say i++ in the script. – user2610063 Jul 23 '13 at 10:20
  • What version of IE are you working with? Note that `Array.forEach` was only added in IE9; earlier versions do not support it. (but there are other ways of looping through an array, so it's no big deal) – Spudley Jul 23 '13 at 10:33
  • +1 to @Pedro3M : although I'd add to look at the highest-rated answer, not the accepted answer: – founddrama Jul 23 '13 at 10:44

1 Answers1

5

Just place this shim from MDN at the beginning of your scripts:

if ( !Array.prototype.forEach ) {
  Array.prototype.forEach = function(fn, scope) {
    for(var i = 0, len = this.length; i < len; ++i) {
      fn.call(scope, this[i], i, this);
    }
  }
}
VisioN
  • 143,310
  • 32
  • 282
  • 281
  • @founddrama What? `oO` FYI there is no difference if it is `++i` or `i++` in the last section of `for`, since this code is executed separately after last line in the `for` block. – VisioN Jul 23 '13 at 10:47
  • Thanks for the help everyone. I managed to get it working :) Had to change it quite a bit, but the links and advise provided pointed me in the right direction. Thanks again – user2610063 Jul 23 '13 at 18:24
  • @user2610063 If the answer helped you, you may accept it by clicking tick on the left. – VisioN Jul 23 '13 at 19:49