0

I am taking user input from a form and trying to create a table from that input. The input should determine the number of rows and columns. I have tried the following but I am getting nothing. I am a bit stumped. Any help appreciated.

JS

function makeChart(){
    var table = document.createElement("table");
    var taskName = document.getElementById("taskname").value + "</br>";
    var numDays = document.getElementById("days").value + "</br>";
    var howOften = document.getElementById("times").value + "</br>";
    var rows=table.insertRow(howOften);
    var cols=rows.insertCell(numDays);
    document.getElementById("holdTable").appendChild(table);
    table.appendChild(rows);
    table.appendChild(cols);
}

HTML

<div id="holdTable">
    <form id="chartInput">
        <label for="taskname">Task</label>
        <input id="taskname" type="text" placeholder="Enter the task name here"> <br>

        <label for="days">How many days</label>
        <input id="days" name="days" type="number" min="1" max="7"> <br>

        <label for="times">How many times a day</label>
        <input id="times" name="times" type="number" min="1" max="4"> <br>

        <input id="createChart" type="button" value="Make the chart" onClick="makeChart();"> <br>
    </form>
</div>
2
  • 2
    Did you turn on the error console? This is a lifesaver as it can report errors where Javascript would normally quietly fail. Commented Dec 28, 2012 at 13:46
  • I am using Eclipse so have the log on. Can't open keycharmap file and a JSCallback error. Don't know if these are relevant. Commented Dec 28, 2012 at 14:11

2 Answers 2

1

I think you need to use loop.

for(var i = 1;i<=howOften;i++)
{
    var row = table.insertRow(-1)
    for(var i = 1;i<=numDays;i++)
    {
        row.insertCell(-1)
    }
    table.appendChild(row);//edited
}

Try using this in place of this 2 lines:

var rows=table.insertRow(howOften);
var cols=rows.insertCell(numDays);

EXPLANATION
insertRow function inserts new table row(<tr>) in at index position specified in brackets. It inserts only one row. So if index is 0 <tr> will be appended to the beggining of the table as a first row. Similarly -1 appends <tr> as the last row in the table.

insertCell function inserts cell(<td>) in at index position of the row(<tr>). Same as above -1 means it adds new cell at the last position in the row.

Each row than has to be appended to the table or stored in an array for later use therefore I added table.appendChild(row); which adds each consecutive row to the array.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Dharman, I have added the loop in place of those lines as suggested. As I understand it this will loop through the values and create the number of rows entered and the number of times (at first position?). I am getting an error now stating that rows is not defined. I presume I need to create a row variable and change the table.appendChild line too.
Plus 1 for understanding what the user wanted and showing him the nested loops. I do think your answer could have provided some more information and explanation, however.
0

table.insertRow and tableRow.insertCell take, as their parameters, the index at which you wish to do the insertion. You are, instead, passing a string which contains your field values appended to an HTML BR tag (a malformed one at that).

Furthermore, to add text to an element you need to create text nodes with document.createTextNode and append those nodes to the element in question.

Finally, cols should be a child of rows, not of table. So your line, table.appendChild(cols) is in error as well.

Beyond those comments, I cannot go further. I am not sure what exactly you're looking for in the outputted table structure. It seems to me you are wanting one row with two columns, but Dharman's answer shows that he reads it as you wanting multiple rows. Consider updating your question with some markup showing the desired table.

Edit: Using your comment for more information, I wrote the following code demo for you to try: http://jsfiddle.net/Kkb7n/ . For the sake of demonstration, I have it use the "task name" as the text content of the created cells.

Modified JS in case JSFiddle is down:

var makeChart = function () {
    var table = document.createElement('table'),
        taskName = document.getElementById('taskname').value,
        numDays = document.getElementById('days').value, //columns
        howOften = document.getElementById('times').value, //rows
        row,
        r,
        col,
        c;

    for (r = 0; r < howOften; r++) {
        row = table.insertRow(-1);

        for (c = 0; c < numDays; c++) {
            col = row.insertCell(-1);
            col.appendChild(document.createTextNode(taskName));
        }
    }

    document.getElementById('holdTable').appendChild(table);
};

3 Comments

OK thanks. The number of rows depends on the figure the user inputs to times and the number of columns depends on the number of days. I am trying to take those values, pass them to the table and generate it, so I think the loop is probably what I want. I will keep at it. If I have any success, I will post back.
JAAulde, thanks very much for the fiddle. That's what I am trying to do. As it turns out, I was nearly there, but after comparing your code and mine I can see how I have added a bit extra in the loops (which was probably breaking it) and yours is more efficient in set up. Thanks again. Now I just have to figure out how to allow user to add image to the nodes. That should be fun! P.S. I tried to vote up but don't have enough street cred!
Glad it helped. You ought to be able to accept either my answer or the other. I forget when voting ability kicks in.

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.