2

I have 4 divs. All of them float:left; For example, they can be:

[div id=1][div id=2][div id=3][div id=4]

or

[div id=1][div id=2][div id=3]
[div id=4]

In the second case the last div is put next line as there is no space for it in parent. How can I check if div with id=4 is next line. Pure js or jQuery. Please, help.

isherwood
  • 58,414
  • 16
  • 114
  • 157
  • See the following which may make this question a duplicate (all be it a little more specific) - http://stackoverflow.com/questions/13929972/absolute-position-of-an-element-on-the-screen-using-jquery – Kolban Dec 05 '14 at 20:18
  • 1
    possible duplicate of [Determine wrap location in floated elements](http://stackoverflow.com/questions/11401024/determine-wrap-location-in-floated-elements) – isherwood Dec 05 '14 at 20:19

1 Answers1

5

There is no easy way, you have to check for the position. Comparing the bottom value of element A to the top value of the element B is the safest method because errors can occur and the top value isn't alway the same.

This code would check if div 4 is in an other line than div 1 :

var a = $('#1'), b = $('#4');

var
posA = a.offset().top + a.height(),
posB = b.offset().top;

if(posB > posA){
    //Div 4 is in an other line.
}
Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
  • 1
    Thank you for your time. I think your solution is the best. However, somebody here gave such answer:if ($("current").next("div").attr("id") == "4"). I checked and it seems to work. However this answer was removed. Can you comment? –  Dec 05 '14 at 20:33
  • 1
    @PashaTurok Yes, because that doesn't check if the div is in a new line, but only check if the next div has the id attribute set to 4. – Karl-André Gagnon Dec 05 '14 at 20:35