About document.getElementById() - Simple question for you pros in here!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bjorndal
    New Member
    • Feb 2008
    • 4

    About document.getElementById() - Simple question for you pros in here!

    I'm new to javascript, so I figure som of you guys in here can answer my question pretty easily:

    I have this code which works:
    [HTML]<script type="text/javascript">

    </script>

    </head>

    <body>

    <table>
    <tr>
    <th style="text-align: center;"> Adresse </th>
    <th style="text-align: center;"> Salgspris </th>
    <th style="text-align: center;"> Beboelsesareal </th>
    </tr>
    <tr>
    <td> Klostergade 2 3. sal - 8000 Aarhus C </td>
    <td> 2.495.000 kr </td>
    <td> 119 kvadratmeter </td>
    </tr>
    <tr>
    <td> Volden 12 3. sal - 8000 Aarhus C </td>
    <td> 2.698.000 kr </td>
    <td> 97 kvadratmeter </td>
    </tr>
    <tr>
    <td> Vester Alle 27 2. sal tv - 8000 Aarhus C </td>
    <td> 2.795.000 kr </td>
    <td> 92 kvadratmeter </td>
    </tr>
    </table>

    <form action="#" name="udregn">

    <p> Indtast prisen <input id="pris" type="text" name="pris"> </p>
    <p> Indtast areal <input id="areal" type="text" name="areal"> </p>
    <p> <input id="meterpris" type="text" name="meterpris " disabled> kr pr. kvadratmeter </p>
    <p> <input id="udregn" type="button" value="Beregn kvadratmeterpri sen"
    onClick="docume nt.udregn.meter pris.value = document.udregn .pris.value / document.udregn .areal.value"> </p>

    </form>
    </body>
    </html>
    [/HTML]
    But I want to separate my javascript from my html and I tried som different things like this:

    [html]<script type="text/javascript">

    window.onload = function ()
    {
    document.getEle mentById("udreg n").onClick =
    function()
    {

    document.getEle mentById("pris" ).value / document.getEle mentById("areal ").value
    }
    }

    </script>

    </head>

    <body>

    <table>
    <tr>
    <th style="text-align: center;"> Adresse </th>
    <th style="text-align: center;"> Salgspris </th>
    <th style="text-align: center;"> Beboelsesareal </th>
    </tr>
    <tr>
    <td> Klostergade 2 3. sal - 8000 Aarhus C </td>
    <td> 2.495.000 kr </td>
    <td> 119 kvadratmeter </td>
    </tr>
    <tr>
    <td> Volden 12 3. sal - 8000 Aarhus C </td>
    <td> 2.698.000 kr </td>
    <td> 97 kvadratmeter </td>
    </tr>
    <tr>
    <td> Vester Alle 27 2. sal tv - 8000 Aarhus C </td>
    <td> 2.795.000 kr </td>
    <td> 92 kvadratmeter </td>
    </tr>
    </table>

    <form action="#" name="udregn">
    <p> Indtast prisen <input id="pris" type="text" name="pris"> </p>
    <p> Indtast areal <input id="areal" type="text" name="areal"> </p>
    <p> <input id="meterpris" type="text" name="meterpris " disabled> kr pr. kvadratmeter </p>
    <p> <input id="udregn" type="button" value="Beregn kvadratmeterpri sen"> </p>
    </form>
    </body>
    </html>[/html]

    It doesnt seem to work though - can I get som help?
    Last edited by gits; Feb 7 '08, 10:47 PM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    try the following:

    [CODE=javascript]document.getEle mentById("udreg n").onclick = function() {
    document.getEle mentById("meter pris").value =
    document.getEle mentById("pris" ).value
    / document.getEle mentById("areal ").value;
    } [/CODE]
    note: onclick, and the output to the correct field

    kind regards

    Comment

    • Bjorndal
      New Member
      • Feb 2008
      • 4

      #3
      I'm stille having problems getting any output to the "meterpris"-field.

      Any other suggestions?

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        Originally posted by Bjorndal
        I'm stille having problems getting any output to the "meterpris"-field.

        Any other suggestions?
        post the code you actually use ... the shown code worked so there has to be another error ...

        using this:

        [CODE=javascript]window.onload = function() {
        document.getEle mentById("udreg n").onclick = function() {
        document.getEle mentById("meter pris").value =
        document.getEle mentById("pris" ).value
        / document.getEle mentById("areal ").value;
        }
        }
        [/CODE]
        should work :)

        kind regards

        Comment

        • Bjorndal
          New Member
          • Feb 2008
          • 4

          #5
          It does work :-) Thank you for your help!

          regards

          Comment

          Working...