0

I have two textfield and one button. When user clicked the button, It calls a function and print a table inside a div with given number of rows and columns.

You can see my code below, but this is not working as expected.

HTML

    Rows <input type="text" id="rows">
    Columns <input type="text" id="columns">

    <input type="button" value="Create Table" onClick="printTable();">

    <div id="box"></div>

Javascript

    function printTable()
    {
        var nRows=document.getElementById("rows");
        var nColumns=document.getElementById("columns");
        var spaceofDiv=document.getElementById("box");

        spaceofDiv.innerHTML=("<table border=1>");
        for(i=0; i<nRows.value; i++)
        {
            spaceofDiv.innerHTML=("<tr>");
            for(j=0; j<nColumns.value; j++)
            {    
                spaceofDiv.innerHTML=("<td width=50 height=50>&nbsp;");
            }
        }
        spaceofDiv.innerHTML=("</table>");     

    }
2
  • "this is not working as expected": what do you expect it to do? What do you get instead? I see you are new. Remember: the more details you give, the easier you will get a reply and an accurate answer. Commented Dec 27, 2013 at 13:30
  • 1
    Next time i will do a better explanation! Commented Dec 27, 2013 at 13:43

2 Answers 2

1

You need to remember to

A) Close your table row and table cell elements

B) Concatenate the value of the table markup, as you are currently overwriting your changes with each assignment

    var markup = '';
    markup = "<table border=1>";
    for(i=0; i<nRows.value; i++)
    {
        markup += "<tr>";
        for(j=0; j<nColumns.value; j++)
        {    
            markup += "<td width=50 height=50>&nbsp;</td>";
        }
        markup += "</tr>";
    }
    markup = "</table>";     

    spaceofDiv.innerHTML = markup;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank's for your attention and your time!
Sorry. About to give you +1 but by mistake. My bad !
0

Try some thing this. Use a avariable add all string in to it and then set innerhtml. Als oyou are not closing the tr

function printTable()
    {
        var nRows=document.getElementById("rows");
        var nColumns=document.getElementById("columns");
        var spaceofDiv=document.getElementById("box");
        var tableStr = "";
        tableStr =("<table border=1>");
        for(i=0; i<nRows.value; i++)
        {
            tableStr +="<tr>";
            for(j=0; j<nColumns.value; j++)
            {    
                tableStr +="<td width=50 height=50></td>&nbsp;";
            }
            tableStr +="</tr>"; 
        }
        tableStr  += "</table>"
        spaceofDiv.innerHTML=tableStr;     

    }

2 Comments

You might want to close the table cell elements as well.
I do it now. It's a mine bad habit

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.