Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2003
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question dynamically accessing form elements in loops

    Ok I have a dynamically created form and need to do a verification check on it. I could write out a switch statment to do this however I'm starting to have long downloads simply because of the number of statements. I work mainly in .asp so I'm wondering if I can dynamically effect the form element I'm checking within the following loop. Each form element is named Student# with the # being a number from 1 to gsize.

    in the example below I've put a # in where I'd need to effet the verification.

    for (i = 1; i < gsize; i++){
    if ( document.frmGroupSelect.Student#.value == "NotSelected" )
    {alert ("Group member "+i+"has not been selected. Please complete the group"); return false;}}

    Any help is apriciated. Thanx

    Jon.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    var f = document.frmGroupSelect;
    for (i = 1; i < gsize; i++){
    if ( f.elements['Student'+i].value == "NotSelected" )
    {
    alert ("Group member "+i+" has not been selected. Please complete the group");
    return false;
    }
    }

    I put the form reference to a variable outside the loop for efficiency...

    But if the elements StudentN is a <select> element not a textbox, you need to do it this way for it to work cross-browser.

    var f = document.frmGroupSelect;
    for (i = 1; i < gsize; i++){
    if ( f.elements['Student'+i].options[f.elements['Student'+i].selectedIndex].value == "NotSelected" )
    {
    alert ("Group member "+i+" has not been selected. Please complete the group");
    return false;
    }
    }

    I have a feeling it's a <select> by looking at the code. I could be wrong of course...

  • #3
    New to the CF scene
    Join Date
    Oct 2003
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yea its a set of up to 10 dynamicaly created greated dropdown menus, which need to be selected and not repeated.

    Nice.

    Cheers for the help it works great, nicew idea with the form referance as a variable as well.


  •  

    Posting Permissions

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