Loop Thru Drop Down boxes....

Discussion in 'Javascript' started by ckerns@REfuse.net, Nov 2, 2006.

  1. Guest

    I have a page with a bunch of drop down boxes. They are named:

    MonEquipAMWk1
    MonEquipPMWk1

    thru

    FriEquipAMWk1
    FriEquipPMWk1

    (this sequence continues until Wk11)

    How do I loop thru these boxes, check their value, & assign an
    alternate value if the selection of a box is selection #1?

    Many thanx!

    jp
     
    , Nov 2, 2006
    #1
    1. Advertising

  2. RobG Guest

    wrote:

    > I have a page with a bunch of drop down boxes. They are named:
    >
    > MonEquipAMWk1
    > MonEquipPMWk1
    >
    > thru
    >
    > FriEquipAMWk1
    > FriEquipPMWk1
    >
    > (this sequence continues until Wk11)
    >
    > How do I loop thru these boxes, check their value, & assign an
    > alternate value if the selection of a box is selection #1?


    To loop through the elements, pass a reference to the form to:

    function checkSelects(form){
    var c1 = ['Wk1','Wk2','Wk3','Wk4','Wk5','Wk6',
    'Wk7','Wk8','Wk9','Wk10','Wk11'];
    var c2 = ['Mon','Tue','Wed','Thu','Fri'];
    var c3 = ['AM','PM'];
    var sel;
    for (var i=0, len1=c1.length; i<len1; i++){
    for (var j=0, len2=c2.length; j<len2; j++){
    for (var k=0, len3=c3.length; k<len3; k++){
    sel = form.elements[ c2[j] + 'Equip' + c3[k] + c1 ];

    /* Check the selected option's value or text (or both)
    and set the selectedIndex property to whatever
    */

    }
    }
    }
    }

    A simpler method is to enclose the selects in a div, then use a
    reference to the div and getElementsByTagName to get the selects and
    loop through them:

    function checkSelects(divID){
    var div = document.getElementById(divID);
    var sel, sels = div.getElementsByTagName('select');
    for (var i=0, len=sels.length; i<len; i++){
    sel = sels;
    /* do stuff with sel as above */
    }
    }

    Feature detection omitted for brevity, don't forget to include it.


    --
    Rob
     
    RobG, Nov 2, 2006
    #2
    1. Advertising

  3. ASM Guest

    a écrit :
    > I have a page with a bunch of drop down boxes.


    What is a drop down box ?

    > They are named:
    >
    > MonEquipAMWk1
    > MonEquipPMWk1
    >
    > thru
    >
    > FriEquipAMWk1
    > FriEquipPMWk1
    >
    > (this sequence continues until Wk11)
    >
    > How do I loop thru these boxes, check their value, & assign an
    > alternate value if the selection of a box is selection #1?


    #1 or first choice ?

    > Many thanx!


    Sorry but without calling things by their names ...








    If you are speaking of elements 'select' of a form and of their selected
    'options', (all selects as drop down lists) :

    Javascript :

    function getSelected(myForm) {
    var f = (typeof (myForm) == 'string')?
    document.forms[myForm] || document.getElementById(myForm) :
    myForm;
    var t = '';
    for(var i=0; i<f.length; i++) {
    if(f.tagName.toLowerCase()=='select') {
    t += f.name+' = ';
    var k = f.selectedIndex;
    if(k==0) t += 'default';
    else t += f[k].value;
    t += '\n';
    }
    }
    return t;
    }

    Html :

    <a href="#" onclick="alert(getSelected('form_1')); return false;">
    form 'form_1' choices</a>
     
    ASM, Nov 3, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SirPoonga
    Replies:
    2
    Views:
    799
    Ben Strackany
    Jan 7, 2005
  2. weiwei
    Replies:
    0
    Views:
    1,047
    weiwei
    Jan 5, 2007
  3. msimmons
    Replies:
    0
    Views:
    514
    msimmons
    Jul 16, 2009
  4. Replies:
    5
    Views:
    295
  5. Replies:
    3
    Views:
    312
Loading...

Share This Page