3

I want to create a table of drop down lists dynamically. Here is the procedure: 1) A select box appears with few options in the first cell of first row in a table. 2) When the user clicks on an option, another select box appears in the second cell of the first row. 3) In the third cell, there has to be a button clicking on which will add a new row with the select box as said in 1st point.

<html>
<head>
    <title>Search</title>
    <script type="text/javascript">
        var noRows = 1;
        function func(){
            var t = document.getElementById('searchTable');
            var r = table.insertRow(noRows);
            var col = row.insertCell(0);
            var colNames = document.createElement('select');
            colNames.id = 'colNames0';
            colNames.name = 'colNames' + noRows;
            colNames.onchange = populate;
            colNames.options[0] = new Option('Select','');
            for(var i = 0;i < cols.length;++i){
                colNames.options[i+1] = new Option(cols[i],ids[i]);
            }
            colCell.appendChild(colNames);
        }
        function populate(){
            var sel = document.getElementById("colNames" + noRows);
            var col = sel.options[sel.selectedIndex].id;
            if(col == "int"  || col == "date" || col == "time")
                populateInt();
            else
                populateString();
        }
    </script>
</head>
<body onload="func();">
    <table id="searchTable">
    </table>    
</body>

This is the code I began with. I created the select box dynamically in first cell of first row but unable to get the selected option and create the further select lists.

1 Answer 1

1

I don't see int,date or time assigned as id to any option there and another thing

var col = sel.options[sel.selectedIndex].id;

Instead of this line, you can write like below

var col = sel.options[sel.selectedIndex].getAttribute('id');
Sign up to request clarification or add additional context in comments.

3 Comments

The array ids' has the id of each element. It can be int, date or time.
var sel = document.getElementById('colNames'+noRows); var col = sel.options[sel.selectedIndex].getAttribute('id'); document.write("TEST " + col + " TEST"); I tried printing the value and it prints "TEST null TEST"
It worked. Its actually getAttribute('value'). Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.