2

Hi I need to get position of all element that have attribute "data-branch" per parent - .components

<div class="component">
  <ul class="component--list">
    <li data-branch="1"><p>item<p></li>
    <li><p>item<p></li>
    <li>
      <p>item<p>
      <ul class="component--list">
        <li><p>item<p></li>
        <li data-branch="2"><p>item<p></li>
      </ul>
    </li>
    <li><p>item<p></li>
    <li><p>item<p></li>
  </ul>
</div>

I can't use offset() because I have another elements on page with changing hight like expander etc. I must get position only per .components

$('[data-branch="'+ branch.attribute +'"]').each(function(){
                var position  = $(this).position();
                console.log("Top: " + position.top + " Left: " + position.left);
            });

Result:

Top: 594.515625 Left: 90 
Top: 155.625 Left: 90 

Top: 155.625 Left: 90 because position() get position relative to .component--list not to .component :/

JanuszFrontEnd'u
  • 451
  • 6
  • 14
  • 1
    *"I need"* Cool. So what have you tried so far? – NewToJS Oct 25 '16 at 10:46
  • Position relative to what? parent, document, window ... – Flyer53 Oct 25 '16 at 10:51
  • relative only to .component – JanuszFrontEnd'u Oct 25 '16 at 10:53
  • `position` returns a result relative to the offset parent (i.e., the closest ancestor element with a position different from the default static.) So if you need to get the position relative to an element that has offset parents in between on the way down to the target element, then you need to go and add those different offsets up. (Or you get the position relative to the document for both, and then work with the difference.) – CBroe Oct 25 '16 at 11:08
  • First get the coords of .component--list using .position() and then use .position() again to get a data-branch position and add the values up. – Flyer53 Oct 25 '16 at 11:09

1 Answers1

0

What about jQuery UI position ?

$('[data-branch="2"]').position({of: '.component'})

in resut jquery ui change position data-branch="2" an change position relative to .component :/ but i need to get coords.. How to use position UI to get top and left?

JanuszFrontEnd'u
  • 451
  • 6
  • 14