www.webdeveloper.com
Results 1 to 7 of 7

Thread: The undefined Array

  1. #1
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    24

    The undefined Array

    I have a problem with form, I made and the array. It's the code:

    <script type="text/javascript">
    function error(blad) {
    if (errfound) return;
    var inp1 = document.getElementsByName('aa');
    for(i=0;i < blad.length;i++ ){

    inp1[i].innerHTML = blad[i];}

    errfound = true;
    }
    function validate() {
    errfound = false;
    var blad = new Array();
    with (document.forms[0]) {



    for(j=0;j < 3;j++ ){
    if (no[j].value == "good ansver") { blad[j] += "good";}
    else if(no[j].value == "") {blad[j] += "no ansver";}
    else {blad[j] += "wrong ansver"; }
    }


    if (blad != "" ) error(blad);
    }
    return !errfound;
    }
    </script>

    <form action="#" method="post" onsubmit="return validate();">

    <p>1) Adrian ma na imię: </p>
    <input type="text" name="no"/><br/>
    <span name="aa"></span><br/>
    <p>2)What is the Adrian's name? </p>
    <input type="text" name="no"/><br/>
    <span name="aa"></span><br/>
    <p>3) Wie Adrian haisst du? </p>
    <input type="text" name="n"/><br/>
    <span name="aa"></span><br/>

    <input type="submit" value="wyślij" />
    </form>
    The problem is that with printed sentence from "blad[]" it shows "undefined".
    Last edited by heldan; 08-19-2010 at 10:11 AM.

  2. #2
    Join Date
    Aug 2007
    Posts
    3,767
    document.forms returns a list of forms on the page, not inputs. So give the form a name, and use document.forms.whatever.no[j]. And don't use with.
    Great wit and madness are near allied, and fine a line their bounds divide.

  3. #3
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    24
    But, it works properly, problem is, as I said, witch Array "blad[]". When I;m printing element from blad[] there is added "Undefined"

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Typo
    Code:
    <input type="text" name="n"/><br/>
    So that you have now only 2 elements in array with the name="no", but you have set the length to 3

    But your code is not so well formatted. with() is a dangerous preemption. And you should rather use the length of the collection as a limit (or to use a while loop)

  5. #5
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    24
    So instead of:
    Code:
    with (document.forms[0]) {}
    I have written:
    Code:
    var no = document.getElementsByName('no');
    and, for my surprise, it still isn't working properly.
    Can you tell me what is going on?

  6. #6
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    blad[j] is the way to reference to an array element but not to add a new element to the array. to add elements they use blad.push(here goes a new element);
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  7. #7
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    or blad[blad.length] = new_ele;
    Computer programmer / web developer.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles