Javascript push not working (what am I doing wrong?!)

What I’m doing is making scalable code. I’m getting all the fieldsets in the document and passing them through a regular expression. The ones which I don’t want are dropped, but the ones that match the expression are put through and ‘pushed’ (array.push(i)) into the array ‘rovers’.

As you can see, there’s a lot of alerts here (somehow I find it easier than firebug console, don’t ask) so I know what’s being passed.

  1. The regex is catching the right fieldsets
  2. They are being passed as objects
  3. They are going to the right part of the function
  4. They are being pushed

…however, they are overwriting each other (all being stored in rovers[0])

This has been driving me mad!! It’s such a simple bit of code, but I just can’t get it to work!

Any help would be REALLY appreciated!! :slight_smile:


Jim :slight_smile:

var fieldsets = document.getElementsByTagName("FIELDSET");
    alert('Number of fieldsets: '+fieldsets.length);
    alert('fieldsets[0]: '+fieldsets[0].innerHTML)
    alert('fieldsets[1]: '+fieldsets[1].innerHTML)
    alert('fieldsets[2]: '+fieldsets[2].innerHTML)
    alert('fieldsets[3]: '+fieldsets[3].innerHTML)

    var numberOfFieldsets = fieldsets.length;
    for(i=0; i<numberOfFieldsets; i++){
        var fieldset = fieldsets[i];
        var fieldsetID =;
        alert('fieldset id: '+fieldsetID);
        var idChecker = /roverDetails[\\d]/g;
        var result = fieldsetID.match(idChecker);
        //alert('result: '+result);

        var rovers = new Array();

        if(result == null) {
            alert("was null");
        } else {
            if((result.length != 1)){

            } else {
        //alert('i: '+i);
//        alert('length of rovers array: '+rovers.length);

    alert('rovers[0]: '+rovers[0].innerHTML)
    alert('rovers[1]: '+rovers[1])
    alert('rovers[2]: '+rovers[2])
    alert('rovers[3]: '+rovers[3])

This is because you’re defining rovers inside the loop. So “rovers” becomes a blank, new array with each iteration.

You need to define your rovers empty array before the for loop starts.

Here’s the issue.

var rovers = new Array();

Each and every time a fieldset is checked in the loop, the rovers variable is being reset to an empty array.

Here too is a cleaned up version of the code without the alerts, that has been passed through (with the “good parts” enabled) to ensure that many other issues are dealt with too.

var fieldsets = document.getElementsByTagName("FIELDSET"),
    numberOfFieldsets = fieldsets.length,
    idChecker = /roverDetails[\\d]/g,
    rovers = [],

for (i = 0; i < numberOfFieldsets; i += 1) {
    fieldset = fieldsets[i];
    fieldsetID =;
    result = fieldsetID.match(idChecker);
    if (result && result.length === 1) {

LMAO! Such an idiot! Hey kids, don’t code with the flu!

Thanks Raffles, that’ll be the one. And thanks pmw57, my syntax needs some real work I think - I use JSLint sometimes, I will use it more! :slight_smile:

Cheers guys!! :slight_smile: