Javascript simple question

This is a discussion on Javascript simple question within the Tech Board forums, part of the Community Boards category; Sorry I know this place is mainly for C. But I don't really want to have to register to another ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    12

    Javascript simple question

    Sorry I know this place is mainly for C. But I don't really want to have to register to another site for this JS question. (I'm sure a lot of you have dealt with websites anyway )

    This question is pretty simple, but I just can't figure out why the FOR loop breaks. To me it seems like a bug. It simply breaks after 1 iteration.

    I created a dummie page, for you to see. Simply save it as a .html and run it

    Code:
    <html><head><script type="text/javascript">
    
    function validateForm()
    {
    
    var fields = new Array;
    var fields =[
    document.getElementById("one"),
    document.getElementById("two"),
    document.getElementById("three")];
    
      for (var i = 0; i < parseInt(fields.length); i++) {
        alert(i);
    
        if ( fields[i].value == "" ) {
          alert("Field" + (i+1) + " has no value");   
        }
     
      }
    
    }
    </script></head><body>
    
    <form name="form1">
    Field 1 enter: <input type="text" id="one"> <br />
    Field 2 enter: <input type="text" name="two"> <br />
    Field 3 etner: <input type="text" name="three"> <br /><br />
    
    <input type="button" onclick="validateForm()" value="Submit" />
    
    </form></body></html>
    The error occurs on fields[i].value .. The error is the var i
    You see, if you replace i with 0: fields[0].value, it will successfully loop 3 times.
    I just don't understand why using that variable, fails?

    I tried so many things, moving the script to bottom of page, converting value of i to a char, etc. I may of missed something, but if you take a quick look maybe you know this problem. (I know I can resort to doing multiple IF statement, I just want to know why this logical loop doesn't work)
    Last edited by astral; 07-03-2011 at 05:06 AM.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    I don't think i is the problem, from here it looks like some elements of your form are null. I noticed:
    Code:
    Field 1 enter: <input type="text" id="one"> <br />
    Field 2 enter: <input type="text" name="two"> <br />
    Field 3 etner: <input type="text" name="three"> <br /><br />
    You should use id for all of these. When I changed it, getElementById worked. I think there is something like getElementByName, but you need to pick one or the other.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    12
    Quote Originally Posted by whiteflags View Post
    I don't think i is the problem, from here it looks like some elements of your form are null. I noticed:
    Code:
    Field 1 enter: <input type="text" id="one"> <br />
    Field 2 enter: <input type="text" name="two"> <br />
    Field 3 etner: <input type="text" name="three"> <br /><br />
    You should use id for all of these. When I changed it, getElementById worked. I think there is something like getElementByName, but you need to pick one or the other.
    LOL, I'm such a dumbass. I never even noticed for a second, that they were "name" instead of "id", spent the whole of yesterday and today googling javascript var bugs. -___-

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Happens to the best of us.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    I just thought I'd mention there is a Techboard for programming related questions for languages of non-existing forums.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,441
    Thread moved to Tech board.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Why do you use parseInt on the length property of an array? It's already numeric.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple JavaScript Code Issue
    By clegs in forum Tech Board
    Replies: 8
    Last Post: 07-14-2008, 03:11 AM
  2. Question about JavaScript
    By Flakster in forum Tech Board
    Replies: 6
    Last Post: 06-10-2005, 09:37 AM
  3. JavaScript question
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 09-08-2002, 01:46 PM
  4. Javascript Question!!!
    By compjinx in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 02-02-2002, 05:10 PM
  5. Javascript Question!!!
    By compjinx in forum C# Programming
    Replies: 2
    Last Post: 01-30-2002, 12:33 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21