user963725
user963725

Reputation: 121

Jquery find a div with certain offset

Let say i have 10 div's on a page , so what i want is to change the color of the div which has an offset.left = 10 and offset.top = 10.

I am not sure how practical this question is, but i am looking for a jquery code which can help me finding a div with certain offset.

And yes these div's are created dynamically so please do not provide any hacks with html because their position on the page is also dynamic.

Thanks

Upvotes: 0

Views: 467

Answers (2)

SpYk3HH
SpYk3HH

Reputation: 22570

Just so you know, there is a jQuery Utility EXACTLY for this kinda of thing, so you dont have to use .each every time.

It's not necessarily a better solution, but simply, a more "intended" solution as this is why they made this feature. It's called .filter

Use it like so:

$("div").filter(function(i) { 
    return $(this).offset().top == 10 && $(this).offset().left == 10 
});
// will render a jQuery object containing ONLY the divs that matach the return

See example in

jsFiddle Here

Upvotes: 2

GillesC
GillesC

Reputation: 10874

$('div').each(function() {
  var offset = $(this).offset();
  if (offset.left == 10 && offset.top == 0) {
    // do your stuff
  }
});

Upvotes: 2

Related Questions