Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find elements with position:attribute

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?

like image 294
Vito De Tullio Avatar asked Mar 19 '12 11:03

Vito De Tullio


People also ask

How do you find the position of an element in HTML?

Use element. getBoundingClientRect() property to get the position of an element.

How do you find the position of an element at the top?

The correct approach is to use element. getBoundingClientRect() : var rect = element. getBoundingClientRect(); console.

How are the elements positioned?

The positioning of an element can be done using the top, right, bottom, and left properties. These specify the distance of an HTML element from the edge of the viewport. To set the position by these four properties, we have to declare the positioning method.

What is position attribute in CSS?

The position CSS property sets how an element is positioned in a document. The top , right , bottom , and left properties determine the final location of positioned elements.


2 Answers

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/

like image 179
codef0rmer Avatar answered Sep 24 '22 16:09

codef0rmer


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');

like image 27
Patrick McElhaney Avatar answered Sep 25 '22 16:09

Patrick McElhaney