Thread: Javascript simple question

  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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Happens to the best of us.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    Thread moved to Tech board.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,895
    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, 06:10 PM
  5. Javascript Question!!!
    By compjinx in forum C# Programming
    Replies: 2
    Last Post: 01-30-2002, 01:33 PM

Tags for this Thread