Search code examples
javascripthtmlinputdatatablegetelementbyid

The specified value "undefined" is not a valid number


I´m learning for an exam which is about company inventories. I thought i could do myself a favor and learn something new about coding while learning for this exams. Normally i only do frontend with very little javascript at my company.

What i´m trying to do is setting up a table with 2 input rows.

Row 1: Input from me Row 2: Sums of my input

i put in a button that should give the value i enter at Row 1 to another field in Row 2.

When my input type is number im getting this: The specified value "undefined" is not a valid number. The value must match to the following regular expression: -?(\d+|\d+.\d+|.\d+)([eE][-+]?\d+)?

When my input type is text im getting: undefined

I´m pretty sure something about my function is wrong but i can´t figure it out and when i search for my issue i only find more complex cases which i also don´t understand.

Thank´s in advance for any help :)

<script>
    function test() {
        var x = document.getElementById("1").value;
        document.getElementById("ro").value = x;
    }
</script>
</head>
<body>
<table>
    <tr><td><b><button onclick="test()">test</button></b></td></tr>
    <tr><td>&nbsp;</td></tr>
    <tr>
        <td>
            <div id="flex-table">
                    <table id="names">
                    <!--Names--><!--Names-->
                        <caption>A: Vermögen</caption>
                            <tr><th>I. Anlagevermögen</th></tr>
                            <tr><td class="first">Grundstücke</td></tr>
                            <tr><td class="second">Gernotstraße</td> </tr>
                    </table>
                    <table id="input">
                    <!--Input--><!--Input-->
                        <caption>&nbsp;</caption>
                            <tr><th><p>€</p></th></tr>
                            <tr><td>&nbsp;</td></tr>
                            <tr><td><form id="1"><input type="number" value="0"></form></td></tr>
                    </table>
                    <table id="values">
                    <!--Values--><!--Values-->
                        <caption>&nbsp;</caption>
                            <tr><th><p>€</p></th></tr>
                            <tr><td>&nbsp;</td></tr>
                            <tr><td><input id="ro" type="number"value="0" readonly></td></tr>
                    </table>
                </div>
        </td>
    </tr>
</table>

Solution

  • Mistake

    1. First thing id is not only numeric. so you must mix with some string
    2. And document.getElementById("string1") is placed with form not a input.so add id to input instead of form

    <script>
      function test() {
        var x = document.getElementById("string1").value;
        document.getElementById("ro").value = x;
      }
    </script>
    
    
    <body>
      <table>
        <tr>
          <td><b><button onclick="test()">test</button></b></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>
            <div id="flex-table">
              <table id="names">
                <!--Names-->
                <!--Names-->
                <caption>A: Vermögen</caption>
                <tr>
                  <th>I. Anlagevermögen</th>
                </tr>
                <tr>
                  <td class="first">Grundstücke</td>
                </tr>
                <tr>
                  <td class="second">Gernotstraße</td>
                </tr>
              </table>
              <table id="input">
                <!--Input-->
                <!--Input-->
                <caption>&nbsp;</caption>
                <tr>
                  <th>
                    <p>€</p>
                  </th>
                </tr>
                <tr>
                  <td>&nbsp;</td>
                </tr>
                <tr>
                  <td>
                    <form><input id="string1" type="number" value="0"></form>
                  </td>
                </tr>
              </table>
              <table id="values">
                <!--Values-->
                <!--Values-->
                <caption>&nbsp;</caption>
                <tr>
                  <th>
                    <p>€</p>
                  </th>
                </tr>
                <tr>
                  <td>&nbsp;</td>
                </tr>
                <tr>
                  <td><input id="ro" type="number" value="0" readonly></td>
                </tr>
              </table>
            </div>
          </td>
        </tr>
      </table>