Need help with form!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ilovemusic123
    New Member
    • Mar 2009
    • 2

    Need help with form!

    Hi, I have a question about Java. I'm really new to Java so this is not something I'm familiar with..
    My question is, how do I get the values of pos.offsetTop and pos.offsetLeft to show up in my input box??

    Here is the java code:
    Code:
    function getPosition(element) {
             var left = 0;
             var top = 0;
             if(element.offsetParent) {
                 while(element) {
                     left += element.offsetLeft;
                     top += element.offsetTop;
                     element = element.offsetParent;
                 }
             }
             return {offsetLeft: left, offsetTop: top};
    }
      
         var element = document.getElementById('elem');
         var pos = getPosition(element);
         document.Show.X.value = pos.offsetTop;
         document.Show.Y.value = pos.offsetLeft;
         return true
         }
    Here's the HTML:
    Code:
    <div id="elem">My Element</div>
    <form name="Show">
        <input type="text" name="X" value="0" size="4"> X<br>
        <input type="text" name="Y" value="0" size="4"> Y<br>
    </form>
    Can someone please help me fix my code.. I can't seem to get the values from the java into the form.
    Last edited by acoder; Mar 4 '09, 10:26 AM. Reason: Added code tags - part 2
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    firstly, you certainly mean JavaScript (JavaScript != Java)

    secondly, your Javascript code has an error (there is one curly bracket left over and a return statement out of a function).

    well, it could be something with the time, the script is called, but I have too less info to say for sure.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Yes, lines 14-19 should be within its own function which you could call, say, onclick of a button.

      PS. please use [code] tags properly. See How to Ask a Question. Thanks!

      Comment

      Working...