0

This is the JavaScript. The select has an id of 'counties'.

The table is to be inserted into a div called 'up_bottom'.

var leagueArray = [];
function addTeams() {
  var county=document.getElementById("counties");
  var val = county.options[county.selectedIndex].value;
  leagueArray.push(val);
  function display() {
    var table = document.createElement("table");
    for (var i=0; i<leagueArray.length; i++) {
      var row = table.insertRow();
      for (var j=0; j<leagueArray[i].length; j++) {
        var cell = row.insertCell();
        cell.appendChild(document.createTextNode(leagueArray[i]));
      }
    }
    var tableDiv = document.getElementById("up_bottom");
    tableDiv.appendChild(table);
  }
  display();
}
2
  • And? Are you getting errors in the console? What seems to be the problem? Commented Apr 22, 2015 at 13:31
  • Uncaught TypeError: Cannot read property 'appendChild' of null Commented Apr 22, 2015 at 13:47

2 Answers 2

0

Please do the following Steps

  1. Get Selected Value from Dropdown
  2. Put the Selected Value into Array
  3. Create A String of tr element and append it to Table before First tr element
Sign up to request clarification or add additional context in comments.

Comments

0

Try this,

function addTeams(){
    var leagueArray = [];
    var county=document.getElementById("counties");
    for (i = 0; i < county.options.length; i++) {
       leagueArray[i] = county.options[i].value;
    }
    display(leagueArray);
}

function display(leagueArray) {
    var table = document.createElement("table");
    for (var i=0; i<leagueArray.length; i++) {
        var row = table.insertRow();
        for (var j=0; j<leagueArray[i].length; j++) {
            var cell = row.insertCell();
            cell.appendChild(document.createTextNode(leagueArray[i]));
        }
    }
    var tableDiv = document.getElementById("up_bottom");
    tableDiv.appendChild(table);
}

addTeams();

Comments

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.