Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8566913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:45:53+00:00 2026-06-11T17:45:53+00:00

I have a dropdown list on each cell of table row. Now what I

  • 0

I have a dropdown list on each cell of table row. Now what I need is dynamically add more dropdownlist on a particular cells with pressing the add button. Any code snippet to help on this? Thank you.
Below is the codes. I have tried function addSubRow2 but this is very static and I cant move further because beside each combo box I also need a text box and how give each of them an unique id so that later I can identify during validation and submission into php page?

<html>
<head>
    <script language="javascript">
        function addRow(tableID) {

            var table = document.getElementById(tableID);

            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);

            var colCount = table.rows[0].cells.length;

            for(var i=0; i<colCount; i++) {

                var newcell = row.insertCell(i);

                newcell.innerHTML = table.rows[1].cells[i].innerHTML;
                newcell.innerHTML = newcell.innerHTML +"<br> TEST";
                //alert(newcell.childNodes[2].type);
                /*switch(newcell.childNodes[0].type) {
                    case "text":
                            newcell.childNodes[0].value = "";
                            newcell.childNodes[0].id = "input" + rowCount;
                            break;
                    case "checkbox":
                            newcell.childNodes[0].checked = false;
                            newcell.childNodes[0].id = "checkbox" + rowCount;     
                            break;
                    case "select-one":
                        newcell.childNodes[1].selectedIndex = 0;
                        newcell.childNodes[1].id = "col_" + rowCount+"_2"; 
                            break;
                }*/
                /*if(newcell.childNodes[0].type=="button")
                {
                    alert("TEST");
                    newcell.childNodes[0].id=rowCount;
                }*/
            }

            /*var table = document.getElementById(tableID);
            var rows = table.getElementsByTagName('tr');
            for (var i = 0, row; row = table.rows[i]; i++) {
                  row.id="row"+i;
                  row.name="row"+i;
                  var rowName = "row"+i;
               //iterate through rows
               //rows would be accessed using the "row" variable assigned in the for loop
               for (var j = 0, col; col = row.cells[j]; j++) {
                 //iterate through columns
                 //columns would be accessed using the "col" variable assigned in the for loop
                 col.id="col_"+i+"_"+j;
                   col.name="col_"+i+"_"+j;
                 var cels = rows[i].getElementsByTagName('td')[j];
                 var realKids = 0,count = 0;
                 kids = cels.childNodes.length;
                 while(count < kids){
                        if(cels.childNodes[i].nodeType != 3){
                            realKids++;
                        }
                        count++;
                    }
                    ///alert("I : "+i+"   "+"J : "+j+"  "+"realKids :"+cels.childElementCount);
                //alert();
          for(var k=0 ; k<cels.childElementCount ; k++)
          {

            alert("J :"+j+"  "+"K :"+k+"  "+cels.childNodes[k].type);
          }
               }  
            }*/
        }

        function addSubRow2(cell){
            var dropdown = "<SELECT class=\"select\" name=\"country\">\n" +
            "<OPTION value=\"1\">Serial 1<\/OPTION>\n" +
            "<OPTION value=\"2\">Serial 2<\/OPTION>\n" +
            "<OPTION value=\"3\">Serial 3<\/OPTION>\n" + 
            "<OPTION value=\"4\">Serial 4<\/OPTION>" +
            "<OPTION value=\"5\">Serial 5<\/OPTION>" + 
            "<\/SELECT>";
        //var dropdown =  document.getElementById("dataTable").rows[0].cells[2].childNodes[1].cloneNode(true);
        cell.innerHTML += "<br\/ >" + dropdown;
    }


/*alert("COLD ID : "+colID);
            var tdID = document.getElementById(colID);
    var table = document.getElementById(tableID);
    var new_comboBox = table.rows[0].cells[2].childNodes[2].cloneNode(true);


    tdID.appendChild(new_comboBox);*/


        function deleteRow(tableID) {
            try {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;

            for(var i=0; i<rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[0].childNodes[0];
                if(null != chkbox && true == chkbox.checked) {
                    if(rowCount <= 1) {
                        alert("Cannot delete all the rows.");
                        break;
                    }
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }


            }
            var table = document.getElementById(tableID);
            for (var i = 0, row; row = table.rows[i]; i++) {
                  row.id="row"+i;
               //iterate through rows
               //rows would be accessed using the "row" variable assigned in the for loop
               for (var j = 0, col; col = row.cells[j]; j++) {
                 //iterate through columns
                 //columns would be accessed using the "col" variable assigned in the for loop
                 //alert("J : "+j);
                 col.id="col"+i;
                 if(j==0)
                 {

                 }
                 else if(j==1)
                 {

                }
               }  
            }

            }catch(e) {
                alert(e);
            }
        }

    </script>
</head>
<body>
  Begin Location : <select class='select' id="beginLocation" name="beginLocation">
                    <option value="1">Loc 1</option>
                    <option value="2">Loc 2</option>
                    <option value="3">Loc 3</option>
                    <option value="4">Loc 4</option>
                    <option value="5">Loc 5</option>
                </select>
                <p type="text" class=error id='beginLocation_Error'>
    <br\>
    <input type="button" value="Add Row" onclick="addRow('dataTable')" />

    <input type="button" value="Delete Row" onclick="deleteRow('dataTable')" />

    <table id="dataTable" width="350px" border="1">
        <tr>
            <th></th>
            <th>Client</th>
          <th>Location</th>
          <th>Serial</th>
        </tr>

        <tr>
            <td id="col_0_0"><input type="checkbox" name="chk"/></td>
            <td id="col_0_1">
                <select class='select' id="client1" name="client1">
                    <option value="1">Client 1</option>
                    <option value="2">Client 2</option>
                    <option value="3">Client 3</option>
                    <option value="4">Client 4</option>
                    <option value="5">Client 5</option>
                </select><p type="text" class=error id='client_0_Error'>                
            </td>

            <td id="col_0_1">
                <select class='select' id="location1" name="location1">
                  <option value="1">Loc 1</option>
                    <option value="2">Loc 2</option>
                    <option value="3">Loc 3</option>
                    <option value="4">Loc 4</option>
                    <option value="5">Loc 5</option>
                </select>
                <p type="text" class=error id='beginLocation_Error'>                
            </td>

            <td id="col_0_3">
                <input type="button" value="Add Serial" onclick="addSubRow2(this.parentNode);" />
                <br\>
                <select class='select' id="serial_0_1" name="serial_0_1">
                    <option value="1">Serial 1</option>
                    <option value="2">Serial 2</option>
                    <option value="3">Serial 3</option>
                    <option value="4">Serial 4</option>
                    <option value="5">Serial 5</option>
                </select><input type="text" name=txtLoc" id="txtLoc><p type="text" class=error id='serial_0_1_Error'>


            </td>
        </tr>
    </table>

</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:45:54+00:00Added an answer on June 11, 2026 at 5:45 pm

    Try this – DEMO

    JS

    var i = 0;
    var option;
    
    document.getElementById("add").addEventListener("click", addSelect, false);
    
    function addSelect() {
        var select = document.createElement("select");
        select.setAttribute("name", "mySelect" + i);
        select.setAttribute("id", "mySelect" + i);
    
        option = document.createElement("option");
        option.setAttribute("value", "value one");
        option.innerHTML = "ONE";
        select.appendChild(option);
    
        option = document.createElement("option");
        option.setAttribute("value", "value two");
        option.innerHTML = "TWO";
        select.appendChild(option);
    
        document.getElementById("wrapper").appendChild(select);
    
        i++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the dropdown list like this <?php echo $form->dropdownList($customers,'customer_name', CHtml::listData(Customers::model()->findAll(), 'id', 'customer_name'), array(
I have three DropDown List Controls.each dropdown contains the static values 1,2,3,4 what i
I need to retrieve all of the selected values of each dropdown list on
is it possible for each item in a dropdown list to have its own
I have an ASP.net table. In several cells I have two dropdown lists. The
I have three dropdown list each dependent on previous one. How it is possible
I have a dropdown list that stores name/value pairs. The dropdown appears in each
I have an itemList and for each item , a dropdown list of ratings
I have a dropdown list/menu and each option has a value. I would like
I have a dropdown list with a few options and a group of checkboxes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.