javascriptdomdynamic-values

Im trying to get the values for the selected index of a dynamic select. JavaScript


I am working on a dynamic form for my church and I don't have any problems until I try to get the values for the selected index of a dynamic select. The select is generated by the JavaScript DOM. Below is the code block that generates the select as well as a test function to try to access, retrieve the value. I would really appreciate any help you can give. I have look through my library of code books, searched the web for days and can't find anything that will solve the problem. I can retrieve the users input from dynamic text-boxes but can't get anything from the select.

Thank you for your time and help. If you would like to see the complete source code Full JavaScript Code

/************************/

var t_shirt = 0;
    for(var i = 0; i < 1; i++){
        var cell = document.createElement('td');

        var t_shirt_size = document.createElement('select');
        t_shirt_size.setAttribute('name','t_shirt_size');
        t_shirt_size.setAttribute('id','t_shirt'+t_shirt);
        t_shirt_size.setAttribute('class','dynamic_select_shirt');
        var topt0 = document.createElement('option');
        topt0.setAttribute('id','option0');
        topt0.setAttribute('value','T-Shirt Size');
        topt0.innerHTML = 'T-Shirt Size';
        var topt1 = document.createElement('option');
        topt1.setAttribute('id','option1');
        topt1.setAttribute('value','Youth Small');
        topt1.innerHTML = 'Youth Small';
        var topt2 = document.createElement('option');
        topt2.setAttribute('id','option2');
        topt2.setAttribute('value','Youth Medium');
        topt2.innerHTML = 'Youth Medium';
        var topt3 = document.createElement('option');
        topt3.setAttribute('id','option3');
        topt3.setAttribute('value','Youth Large');
        topt3.innerHTML = 'Youth Large';
        var topt4 = document.createElement('option');
        topt4.setAttribute('id','option4');
        topt4.setAttribute('value','Youth X-Large');
        topt4.innerHTML = 'Youth X-Large';
        var topt5 = document.createElement('option');
        topt5.setAttribute('id','option5');
        topt5.setAttribute('value','Adult Small');
        topt5.innerHTML = 'Adult Small';
        var topt6 = document.createElement('option');
        topt6.setAttribute('id','option6');
        topt6.setAttribute('value','Adult Medium');
        topt6.innerHTML = 'Adult Medium';
        var topt7 = document.createElement('option');
        topt7.setAttribute('id','option7');
        topt7.setAttribute('value','Adult Large');
        topt7.innerHTML = 'Adult Large';
        var topt8 = document.createElement('option');
        topt8.setAttribute('id','option8');
        topt8.setAttribute('value','Adult X-Large');
        topt8.innerHTML = 'Adult X-Large';

        t_shirt++;
        //alert('made it past ++ topt ');

        t_shirt_size.appendChild(topt0);
        t_shirt_size.appendChild(topt1);
        t_shirt_size.appendChild(topt2);
        t_shirt_size.appendChild(topt3);
        t_shirt_size.appendChild(topt4);
        t_shirt_size.appendChild(topt5);
        t_shirt_size.appendChild(topt6);
        t_shirt_size.appendChild(topt7);
        t_shirt_size.appendChild(topt8);
        cell.appendChild(t_shirt_size);            
        row.appendChild(cell);
    }    


    table_body.appendChild(row);


}
table_nop.appendChild(table_body);

mul.appendChild(table_nop);


table_nop.setAttribute('id','dynamic_table_main');

document.getElementById('form_div').style.display='block';

/*************************************/ code i have tried to retrieve the value with. /*************************************/

    function a(){
    var cname = document.getElementById('tbNAME'+0).value;
        var cbd = document.getElementById('tbBD'+0).value;
        var cAORD = document.getElementById('tbAORD'+0).value;
        var cgender = document.getElementById('gender_select'+0).selectedIndex;
        var cshirt = document.getElementById('t_shirt'+0);



alert(cshirt.length);
}


function check_dynamic_table(){
        var errorc = 0;

        var nop = check_id_global;
        for(var i =0; i < nop;i++){
        var cname = document.getElementById('tbNAME'+i).value;
        var cbd = document.getElementById('tbBD'+i).value;
        var cAORD = document.getElementById('tbAORD'+i).value;
        var cgender = document.getElementById('gender_select'+i).value;
        var cshirt = document.getElementById('t_shirt'+i).value;

        if(cname == "" || cname == null){
         alert('Please Fill Out All Fields:' +cname );
        }
        if(cbd == "" || cbd == null){
         alert('Please Fill Out All Fields:'+cbd);
        }
        if(cAORD == "" || cAORD == null){
            alert('Please Fill Out All Fields:'+cAORD);
        }
        if(cgender == 'gender'){
            alert('Please make a Gender Selection:'+cgender);
        }
        if(cshirt == 'T-Shirt Size'){
            alert('Please make a T-Shirt Selection:'+cshirt);
        }

        }

}

Solution

  • I figured it out. Plato your post triggered a thought that otherwise may have never come. The var cshirt points to the select element's id, so what i needed to do was to take that variable and access the options element and enter the array of option elements requesting the selectedIndex, then request the value. Now i can get the value of the dynamic select and pass this to my php. I appreciate the other posts but i wanted to do this in pur JavaScript. Below is the solution:

    var cshirt = document.getElementById('t_shirt'+0);
    var cshirt_value = cshirt.options[cshirt.selectedIndex].value;  
    
    alert(cshirt_value);}