-1

I got this code from somewhere on the internet but I don't quite understand it. Especially the for loop part.

function FindPosition(oElement) {
  if( typeof( oElement.offsetParent ) != "undefined" ) {
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
    return [ posX, posY ];
  }
  else {
    return [ oElement.x, oElement.y ];
  }
}

I don't understand particularly the for loop part here:

for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent )

I expected there to be a middle expression or upper bound instead of just oElement

Please can someone explain to me?

2
  • 1
    oElement in the middle part is truthy while element exists. It traverses up through its parents until it gets to the root element, oElement becomes null which is falsy. Commented Aug 1, 2017 at 8:46
  • 2
    @YeldarKurmangaliyev sounds like this code could be written using a while loop, which could make the code easier to reason about. Commented Aug 1, 2017 at 8:57

2 Answers 2

3

A for loop header has 3 parts: initialization, condition, iteration.

Here, the condition (...; oElement; ...) is not in the form of a comparison against an upper bound (e.g. i < 100). Instead, it checks if oElement is true (i.e. not null, not undefined, not 0)

The iteration part(...; oElement = oElement.offsetParent) does not increase any indexing value as you are used to (e.g. i++). Instead, it moves upward in the element hierarchy by assigning oElement's parent to oElement.

The condition makes sure the upward traversal stops when there is no longer an offsetParent defined.

Sign up to request clarification or add additional context in comments.

Comments

2

This is just an example of using for-loop instead of do-while. If you rewrite this using do-while the overall idea behind the loop should be clear.

function FindPosition(oElement) {
  if( typeof( oElement.offsetParent ) != "undefined" ) {
    var posX = 0, posY = 0;
    do {
      // update coordinates using offset data
      posX += oElement.offsetLeft; 
      posY += oElement.offsetTop;

      // move up the DOM tree
      oElement = oElement.offsetParent

    } while(oElement != null) // while there is offsetParent

    return [ posX, posY ];
  }
  else {
    return [ oElement.x, oElement.y ];
  }
}

4 Comments

thanks so much for the explanation. Please I am also not clear about [ oElement.x, oElement.y ]. Does this mean that html elements have x and y coordinates?
@Awa Never heard of it. Where did you get this code from?
from here @YuryTarabanko
Not sure. This code might be written to take care of some non-standard browsers. But I have never heard of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.