By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,626 Members | 1,929 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,626 IT Pros & Developers. It's quick & easy.

Need help with form!

P: 2
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. }
  13.  
  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
Share this Question
Share on Google+
2 Replies


Dormilich
Expert Mod 5K+
P: 8,639
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

acoder
Expert Mod 15k+
P: 16,027
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.