Input field x,y co-ordinates when in a table

I'm working on a JavaScript that will determine the physical location of the input fields on a page.  My script works perfectly fine under most circumstances.  However, if the input field is located inside of a table, the script returns a really odd location for the field.  I've included a very simple page below that demonstrates what I'm talking about.

Does anyone have any insight on why it's doing this and how I can fix it?

====================================

<HTML>
   <HEAD>
      <title>JavascriptControlProblem</title>
   </HEAD>
   <body>
      <form id="Form1">
            <input type="text" id="fieldA"><br>
            <input type="text" id="fieldB"><br>

            <table>
               <tr><td><input type="text" id="field1"></td></tr>
               <tr><td><input type="text" id="field2"></td></tr>
               <tr><td><input type="text" id="field3"></td></tr>
               <tr><td><input type="text" id="field4"></td></tr>
               <tr><td><input type="text" id="field5"></td></tr>
            </table>
           
         <script language="javascript">
               var ctl;
               for(i=0; i<document.forms[0].elements.length; i++){
                  if (document.forms[0].elements[i].type == 'text') {
                     ctl = document.forms[0].elements[i];
                     alert(ctl.id + ' --- ' + ctl.offsetLeft + ', ' + ctl.offsetTop);
                  }
               }
         </script>
     
      </form>
     
   </body>
</HTML>
glass_snakeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LakioCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jens FiedererTest Developer/ValidatorCommented:
I think Lakio has the solution for your problem.

The EXPLANATION for your problem is that the offsets are from the PARENT element.  So if you run

     <script language="javascript">
      var ctl;
      for(i=0; i<document.forms[0].elements.length; i++){
            if (document.forms[0].elements[i].type == 'text') {
                  ctl = document.forms[0].elements[i];
                  alert(ctl.outerHTML+ ' --- ' + ctl.offsetLeft + ', ' + ctl.offsetTop);
                  var j = 1;
                  for (ctl = ctl.parentNode; ctl; ctl = ctl.parentNode) {
                        if (ctl == document.forms[0]) {
                              break;
                        }
                        alert(ctl.outerHTML + ' -' + j + ' - ' + ctl.offsetLeft + ', ' + ctl.offsetTop);
                        j++;
                  }
            }
      }
</script>

instead you will see how for any one element, the real position in the form is the sum of the positions going up toward the form.
0
ZvonkoSystems architectCommented:
0
LakioCommented:
I knew that jensfiederer but Im to lasy so thanks :D
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.