I have a div
which contain about 100 other div
elements. Each of div
element have top
and left
properties. How can I find a div
which have the largest left
properties?
I need to best perfomance. Thanks.
Asked
Active
Viewed 2,874 times
5

user348173
- 8,818
- 18
- 66
- 102
-
I don't think there is a way of getting out of checking all of them... – Lix Jul 09 '12 at 11:12
-
2I think you have to loop through all divs – Shehzad Bilal Jul 09 '12 at 11:14
-
2possible duplicate of `:)` [jQuery: How to select all elements that have a specific CSS property applied](http://stackoverflow.com/questions/1220834/jquery-how-to-select-all-elements-that-have-a-specific-css-property-applied) – Tats_innit Jul 09 '12 at 11:16
1 Answers
3
Try this,
var divWithTopLeft = null;
var maxLeft = 0;
$('div').each(function(){
left = this.style.left.replace('px','');
if(left > maxLeft )
{
maxLeft = left;
divWithTopLeft = this;
}
});

Adil
- 146,340
- 25
- 209
- 204
-
Jfyi, if you define `.class1 { position:relative; }` in the fiddle, then you can also use `this.offsetLeft` property instead of the `this.style.left.replace('px','');` to make the code run a lil bit faster. – Stano Jul 09 '12 at 13:31