4

Please help with below mentioned scenario->

I am having wanna display values from 1 to 30 in a table such a way that nos 1,2,3 should come in one tag and like wise 4,5,6 in other tr tag ans so on till 30 value. I wanna use table for displaying values in a element of a table. wherein each value like "1" should display in one , no "2" should display in different <TD> and so on.

I mean to say that value "1" should be displayed in single <TD> of <Table> tag ,value "2" should be displayed in another <td> tag and so on ,Also after three subsequent <TD>s one <Tr> should be used. Output should be as folllows ->

1 2 3 
4 5 6
7 8 9

and so on!

Early response would be much appreciated. Thanks.

I have tried Code as given below,

<script type="text/javascript">

    document.write("        <table width='673' align='center' cellpadding='2' cellspacing='1'>");
    document.write("            <tr>");
    document.write("    <td valign = 'top'>");
    document.write("                </td>");

    document.write("            </tr>");

    var cnt = 0;
    for (var idx = 1; idx <= 30; idx++) 
    {
        cnt = cnt + 1;
        document.write("            <tr>");
            document.write("    <td valign = 'top'>");
            document.write("        <table width='100px' align='center' cellpadding='2' cellspacing='1'>");
            document.write("            <tr>");
            document.write("                <td align='center'>");
            document.write("                   " + idx + "");
            document.write("                </td>");
            document.write("            </tr>");
            document.write("            <tr>");
            document.write("                <td class='label'>");
            document.write("                    <span> name part : " + idx + "</span>");
            document.write("                </td>");
            document.write("            </tr>");
            document.write("            </table>");
            document.write("                </td>");
            if (cnt = 3) 
            {
                document.write("            </tr>");
            }
            if (cnt = 3) {
                cnt = 0;
            }

        }

    document.write("            </table>");
</script>
4
  • Didn't you just post the same question at stackoverflow.com/questions/9751799/… ? Commented Mar 17, 2012 at 17:10
  • Ya @ j08691 but this time ,I have clarified my query along with detailed explanation of the question! Since my early query was closed without being answered , I have posted new Q. Commented Mar 17, 2012 at 17:11
  • I am confused. At least format the coding part of your question properly. Commented Mar 17, 2012 at 17:15
  • @ Kaf, what else needs to be provided I have used javascript that will be embeded in head section of my Html page,Hope it is now clarified to u? Commented Mar 17, 2012 at 17:17

3 Answers 3

10

You can try something like this:

var mytable = "<table cellpadding=\"0\" cellspacing=\"0\"><tbody><tr>";

for (var i = 1; i < 31; i++) {
  if (i % 3 == 1 && i != 1) {
    mytable += "</tr><tr>";
  }
  mytable += "<td>[" + i + "]</td>";
}

mytable += "</tr></tbody></table>";

document.write(mytable);

Here is a jsFiddle demo

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

Comments

1

This is untested psuedo-code, but may help you get started:

var x, row;
for(x=1;x<10;x=x+row)
{
    document.write('<tr>');
    for(row=0;row<2;row++)
    {
        document.write('<td>' + (x + row));
        // Whatever else you want to output
    }
}

Edit: this answer was given before OP edited his question to add additional info.

Comments

0

Use the modulo operator % to check for 3rd numbers and set the tags accordingly.

http://en.wikibooks.org/wiki/C_Sharp_Programming/Operators

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.