JS arrays

Discussion in 'JavaScript' started by fhatzis, Jan 3, 2009.

  1. #1
    I have the following problem in my jscript program..My programm has the following structure:

    <html>
    <head>
    <title></title>
    <script type="text/javascript">

    var result;

    function func(add)
    {
    result=add+5;
    return result;
    }

    <script>
    </head>

    <form action="#" onsubmit="func(this.add.value); return false">

    <p>

    <input type="text" size="60" name="add"/>

    <input type="submit" value="Go!" />

    </p>

    </form>
    </body>
    </html>





    My problem is that I want to create a table with "result" values. The variable result is being returned as you see when I press the button "Go!" and is calculated in the "func" function. How Can I create this table in the <body> </body> section without knowing the value of the "result" variable ???

    For example:


    I write in the textbox the number 4.Then I press the button Go! Then the function func calculates the result variable and give it the value 4+5=9. (result=add+5).My problem here is that I dont know how to create a table with length = 9..I know how to create generally a table in html language but I dont know how to create it in the <body> </body> section and with a special number as result..

    Please help me if you know how..
    Thank you for your time and I wish u happy new year!!!
     
    fhatzis, Jan 3, 2009 IP
  2. cont911

    cont911 Peon

    Messages:
    50
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Not sure i got you idea, but hope this code helps

    <html>
    <head>
    <title></title>
    <script type="text/javascript">

    var result;

    function func(add)
    {
    result=add+5;
    return result;
    }

    function createTable(val)
    {
    var result = func(val);
    var tableContent = "<TABLE border=1>";
    for(i=0; i < result; i++)
    {
    tableContent +="<TR><TD>" + i + "</TD></TR>";
    }
    tableContent +="</TABLE>";
    var tabDiv = document.getElementById("placeForTable");
    tabDiv.innerHTML = tableContent;
    }

    </script>
    </head>

    <form action="#" onsubmit="createTable(this.add.value); return false">

    <p>

    <input type="text" size="60" name="add"/>

    <input type="submit" value="Go!" />
    </p>

    </form>
    </body>

    <div id="placeForTable">
    </div>


    </html>
     
    cont911, Jan 4, 2009 IP
  3. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It makes things much more readable when you use [html] or [php] or [code] tags.
     
    MMJ, Jan 4, 2009 IP