SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Albany, N.Y.
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    external not working but work embedded in html

    I am trying to use an array to store the names of a form (test) elements (i.e. text boxes) so that I can see if they were left blank.

    the problem I am having is this line of the code

    Code:
    document.test.err[i].value == ""
    firebug my JavaScript debugger is giving me the follow error


    document.test.err has no properties
    [Break on this error] if ( document.test.err[i].value == "" )


    the line should be


    document.test.req1.value == ""

    What am I missing?

    Thanks

    This is the full code
    Code:
            var errmsg = new String();
    	var err = new Array("req1");
    
    	
    	for(i=0;i<err.length;i++) {
    
    		if ( document.test.err[i].value == "" )
    		{
    			errmsg += "Error";  
            	
    		}
    
    	}

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,139
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    code

    this line
    HTML Code:
    var err = new Array("req1");
    creates an array and populates the first key->value, ie. 0 -> "req1"
    In the for loop's first pass, the variable i is equal to 0
    so err[i] equals "req1", hence
    document.test.req1.value == ""

  3. #3
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about
    Code:
    document.test[err[i]].value == ""
    ?

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Albany, N.Y.
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mittineaque,

    I followed you until "document.test.req1.value == """

    I don't think the code even translates err[i] == req1

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Albany, N.Y.
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks I figure it out with your help, I needed to change the code to

    var elementval = document.appts.elements[err[i]].value;


    I had the wrong form name; I guess I should pass the form name in the function. Right know I am using an array to enter the names of the text boxes and drop down boxes is there a way to reference all the elements in a form?

  6. #6

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by cedtech23 View Post
    is there a way to reference all the elements in a form?
    You were using it before, the elements array.

    Check out the following page for further details.
    http://www.quirksmode.org/js/forms.html
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •