13

I would try to find all "absolute" elements in my page; with jQuery I though it would be something like

$('[position="absolute"]')

but on ff 10.0.2 I cannot find an element...

Also, I cannot run the exaple code on http://api.jquery.com/attribute-equals-selector/ Is there something wrong on this syntax?

Vito De Tullio
  • 2,332
  • 3
  • 33
  • 52

3 Answers3

19

You could use filter()

$('*').filter(function(){
   var position = $(this).css('position');
   return position === 'absolute';
});

You can't use attribute equals selector because because that selector would search elements with an attribute called position that equals absolute like this

 <div position="absolute">

but in your case position is a css property

potench
  • 3,802
  • 1
  • 28
  • 39
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
11

Building on Nicola's answer, you can also extend jQuery's selector engine.

$.extend($.expr[':'],{
    absolute: function(el) {
        return $(el).css('position') === 'absolute';
    },
    relative: function (el) {
        return $(el).css('position') === 'relative';
    },
    static: function (el) {
        return $(el).css('position') === 'static';
    },
    fixed: function (el) {
        return $(el).css('position') === 'fixed';
    }
});

Then you can you do things like this.

$(':absolute');

$('div.sidebar:relative');

Patrick McElhaney
  • 57,901
  • 40
  • 134
  • 167
1

Try this:

$("*[style*='position:absolute']").each (function () {
     alert($(this).html());
});

Demo : http://jsfiddle.net/XRRbr/1/

More info: http://api.jquery.com/attribute-contains-selector/

codef0rmer
  • 10,284
  • 9
  • 53
  • 76
  • That can be possible but the code would be tricky. If the accepted answer has worked for you then that must be the valid answer. JFYI, https://developer.mozilla.org/en/DOM/window.getComputedStyle http://stackoverflow.com/questions/754607/can-jquery-get-all-css-styles-associated-with-an-element – codef0rmer Mar 19 '12 at 13:50