471,874 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,874 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. function getPosition(element) {
  2.          var left = 0;
  3.          var top = 0;
  4.          if(element.offsetParent) {
  5.              while(element) {
  6.                  left += element.offsetLeft;
  7.                  top += element.offsetTop;
  8.                  element = element.offsetParent;
  9.              }
  10.          }
  11.          return {offsetLeft: left, offsetTop: top};
  12. }
  14.      var element = document.getElementById('elem');
  15.      var pos = getPosition(element);
  16.      document.Show.X.value = pos.offsetTop;
  17.      document.Show.Y.value = pos.offsetLeft;
  18.      return true
  19.      }
Here's the HTML:
Expand|Select|Wrap|Line Numbers
  1. <div id="elem">My Element</div>
  2. <form name="Show">
  3.     <input type="text" name="X" value="0" size="4"> X<br>
  4.     <input type="text" name="Y" value="0" size="4"> Y<br>
  5. </form>
Can someone please help me fix my code.. I can't seem to get the values from the java into the form.
Mar 1 '09 #1
2 1089
8,658 Expert Mod 8TB
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.
Mar 1 '09 #2
16,027 Expert Mod 8TB
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!
Mar 4 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by cmc_dermo | last post: by
reply views Thread by ward | last post: by
17 posts views Thread by freemann | last post: by
8 posts views Thread by | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.