SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Location
    London
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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!!

    Thanks

    Jim

    Code JavaScript:
    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 = fieldset.id;
            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");
                exit();
            } else {
                alert(fieldsets[i]);
                if((result.length != 1)){
                    exit();
     
                } else {
                     rovers.push(fieldset);
                     alert(i);
                }
            }
            //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])

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,688
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Here's the issue.

    Code javascript:
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,688
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Here too is a cleaned up version of the code without the alerts, that has been passed through jslint.com (with the "good parts" enabled) to ensure that many other issues are dealt with too.

    Code javascript:
    var fieldsets = document.getElementsByTagName("FIELDSET"),
        numberOfFieldsets = fieldsets.length,
        idChecker = /roverDetails[\d]/g,
        rovers = [],
        i,
        fieldset,
        fieldsetID,
        result;
     
    for (i = 0; i < numberOfFieldsets; i += 1) {
        fieldset = fieldsets[i];
        fieldsetID = fieldset.id;
        result = fieldsetID.match(idChecker);
     
        if (result && result.length === 1) {
            rovers.push(fieldset);
        }
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Location
    London
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!

    Cheers guys!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •