Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select HTML element by CSS property value in style attribute

Tags:

jquery

I have HTML like this:

…
<div style="top: 252px; left: 54px;"></div> 
<div style="top: 252px; left: 162px;"></div> 
<div style="top: 288px; left: 108px;"></div>
…

I have a JavaScript object literal like this:

var pos = { top: 252, left: 54 };

I want to select the element with the position that the object literal indicates. Positions are unique, so only one element will be selected.

Thank you for answers.

like image 963
Darkry Avatar asked Jul 26 '11 10:07

Darkry


People also ask

How can we select elements with a specified attribute in CSS?

The [attribute|="value"] selector is used to select elements with the specified attribute, whose value can be exactly the specified value, or the specified value followed by a hyphen (-). Note: The value has to be a whole word, either alone, like class="top", or followed by a hyphen( - ), like class="top-text".

How do you use attribute selectors to style elements?

[attribute$=”value”] Selector: This selector is used to select all the elements whose attribute value ends with the specified value. The value doesn't need to be a whole word. [attribute*=”value”] Selector: This selector selects all the elements whose attribute value contains the specified value present anywhere.

What CSS selector is used to select elements with a specified attribute and value?

The [attribute=value] selector is used to select elements with the specified attribute and value.

How do you retrieve a CSS property value of an element?

The attr() CSS function is used to retrieve the value of an attribute of the selected element and use it in the stylesheet. It can also be used on pseudo-elements, in which case the value of the attribute on the pseudo-element's originating element is returned.


3 Answers

Credit goes to the original poster here;

jQuery find by inline css attribute

The function is 'styleEquals' and can be implemented as;

jQuery.extend(jQuery.expr[':'], {
    styleEquals: function(a, i, m){
        var styles = $(a).attr("style").split(" ")
        var found = false;
        for (var i = 0; i < styles.length; i++) {
                if (styles[i]===m[3]) {
                        found = true;
                        break;
                }
        }
        return found;
    }
});

You can then search elements by their style attribute values using your new jquery extensions function such as;

$("div:styleEquals('top=252px'):styleEquals('left:54px')");
like image 82
Brian Scott Avatar answered Sep 21 '22 20:09

Brian Scott


All I can say is don't do this!

If you can add a unique position style to a div, you can equally easily add an identifier at the same time.

Use this identifier to select the element in javascript, not the css positioning.

like image 33
BonyT Avatar answered Sep 20 '22 20:09

BonyT


You can do it like this with the jQuery JavaScript library out of the box:

var pos = { top: 252, left: 54 };
$('div[style*="top: ' + pos.top + 'px"][style*="left: ' + pos.left + 'px"]');

You need to make sure to use white space consistently. If you type:

<div style="top:252px; left:54px;"></div>

the proposed selector will not work.

You can also add other CSS properties to the style attribute by using my example code, and the order would not matter. Here is an example:

<div style="left: 54px; background: gray; top: 252px;"></div>
like image 41
knut Avatar answered Sep 20 '22 20:09

knut