SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    276
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    loop through all available select boxes.

    Im trying to do a very simple bit of validation in order to check at least one of my select box is something other than the default value.
    The main problem im having is that I dont know how many select boxes im going to have as they are created dynamically and thus I cant program a set number in a for loop. I cant find any reference to a for each loop either and there for im kind of stuck.
    code im using is

    Code:
    
    if (document.main.airport.value == '') {
          error_message = error_message + "* Your arrival airport.\n";
          error = 1;
        }
    all im checking for at the minute is that airport is not empty what i need to do is put that code inside a loop that will run through all of the select boxes
    also how do I find the name of the select box in order to pass it to the check?

    Any suggestions.

    Thanks Chris

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var allselects= document.getElementsByTagName('select');
    var L= allselects.length;
    for(var i=0;i<L;i++){
    var tem= allselects[i];
    var temname= tem.name;
    var temIndex= tem.selectedIndex;
    var temValue= tem.options[temIndex].value;

    // tests
    }

  3. #3
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    276
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrhoo View Post
    var allselects= document.getElementsByTagName('select');
    var L= allselects.length;
    for(var i=0;i<L;i++){
    var tem= allselects[i];
    var temname= tem.name;
    var temIndex= tem.selectedIndex;
    var temValue= tem.options[temIndex].value;

    // tests
    }
    Thnnks for that. although can you spot what I have done wrong here?

    Code:
    var submitted = false;
    
    function check() {
      var error = 0;
      var error_message = "We cannot quote your transfer without this important information\nPlease provide the following essential information:\n\n";
     
      if(submitted){ 
        alert( "This form has already been submitted. Please press Ok and wait for this process to be completed."); 
        return false; 
      }
       
    var allselects= document.getElementsByTagName('select');
    var L= allselects.length;
    for(var i=0;i<L;i++){
    var tem= allselects[i];
    var temname= tem.name;
    var temIndex= tem.selectedIndex;
    var temValue= tem.options[temIndex].value;
    
        if (temValue == 'Select') {
          error_message = error_message + "* A valid departure date.\n";
          error = 1;
        }
      }
    
    }
    
    
    if (error = 1) { 
        alert(error_message); 
        return false; 
      } else { 
      	submitted = true; 
    	return true; 
      } 
    }
    firefox reports invalid return on line 33

    thanks Chris

  4. #4
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to enclose the code in a function to use a return.
    Otherwise, there's nowhere to be returning from- you are already there..

  5. #5
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    276
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrhoo View Post
    You have to enclose the code in a function to use a return.
    Otherwise, there's nowhere to be returning from- you are already there..
    Hi I understand that but I though this code was within a function called check_me()
    can you see where I have went wrong?
    Thanks Chris

    Code:
    function check_me(){
      var error = 0;
      var error_message = "You need to choose at least one transfer.";
     
      if(submitted){ 
        alert( "This form has already been submitted. Please press Ok and wait for this process 
    
    to be completed."); 
        return false; 
      }
       
    var allselects= document.getElementsByTagName('select');
    var L= allselects.length;
    for(var i=0;i<L;i++){
    var tem= allselects[i];
    var temname= tem.name;
    var temIndex= tem.selectedIndex;
    var temValue= tem.options[temIndex].value;
    
        if (temValue == 'Select') {
          error = 1;
        }
      }
    
    }
    
    
    if (error = 1) { 
        alert(error_message); 
        return false; 
      } else { 
      	submitted = true; 
    	return true; 
      } 
    }

  6. #6
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    276
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone?


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
  •