- Using jquery to select an element by it’s style
- 3 Answers 3
- Select Element By CSS style (all with given style)
- Select Element By CSS style (all with given style)
- How to select element by order ?
- Select HTML element by CSS property value in style attribute
- Select HTML element by CSS property value in style attribute
- 7 Answers 7
- Select all elements that have a specific CSS, using jQuery
- 6 Answers 6
Using jquery to select an element by it’s style
I am trying to select an html element that has no classes or id’s, but I still need to select it. Here is what the element looks like:
$("table[style~='background-color: white; border: 1px solid #aaa; box-shadow: 2px 2px 2px rgba(0,0,0,0.2); border-left: 10px solid #1E90FF; margin: 0 auto;']")
you can’t select an element by it’s style attributes in jquery. do you have any parent elements you can target?
There’s some missing whitespace in your selector in the rgba function. I’d suggest adding that and seeing if it works.
Okay, I didn’t know that you couldn’t select an element based on it’s css. I’ll try to find a parent element that I can select.
Of course you can, that statement is wrong. jQuery accepts any CSS syntax, and using style is an attribute selector.
Modify your style and suddenly your js doesn’t work. please avoid this completely. Find another way to solve your problem.
3 Answers 3
The selector is just wrong, you are missing some spaces.
You can use the attribute selector in jQuery for style but have in mind that an inline-style can change if another javascript function affects the element style the selector will not work.
Looking for another selector is much better, look for a class or an id wrapping the table element.
If there is, do something like this:
console.log($('table[style="background-color: white; border: 1px solid #aaa; box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.2); border-left: 10px solid #1E90FF; margin: 0 auto;"]').length);
console.log($('table').filter('[style="background-color: white; border: 1px solid #aaa; box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.2); border-left: 10px solid #1E90FF; margin: 0 auto;"]').length);
Select Element By CSS style (all with given style)
I would assume it is easier to find elements by style where the style is explicitly declared: the style is non-inherited (such as positioning) the style is not the default (as would be position:static). Solution 1: Credit goes to the original poster here; jQuery find by inline css attribute The function is ‘styleEquals’ and can be implemented as; You can then search elements by their style attribute values using your new jquery extensions function such as; Solution 2: All I can say is don’t do this!
Select Element By CSS style (all with given style)
Is there a way to select all elements that have a given style using JavaScript?
Eg, I want all absolutely positioned elements on a page.
I would assume it is easier to find elements by style where the style is explicitly declared:
- the style is non-inherited (such as positioning)
- the style is not the default (as would be position:static).
Am I limited to those rules? Is there a better method for when those rules apply?
I would happily to use a selector engine if this is provided by one (ideally Slick — Mootools 1.3)
EDIT:
I came up with a solution that will only work with above rules.
It works by cycling through every style rule, and then selector on page.
Could anyone tell me if this is better that cycling through all elements (as recommended in all solutions).
I am aware that in IE I must change the style to lowercase, but that I could parse all styles at once using cssText. Left that out for simplicity.
Looking for best practice.
var classes = ''; Array.each(documents.stylesheets, function(sheet)< Array.each(sheet.rules || sheet.cssRules, function(rule)< if (rule.style.position == 'fixed') classes += rule.selectorText + ','; >); >); var styleEls = $$(classes).combine($$('[style*=fixed]'));
You can keep Mootools, or whatever you use. 🙂
function getStyle(el, prop) < var view = document.defaultView; if (view && view.getComputedStyle) < return view.getComputedStyle(el, null)[prop]; >return el.currentStyle[prop]; > function getElementByStyle(style, value, tag) < var all = document.getElementsByTagName(tag || "*"); var len = all.length; var result = []; for ( var i = 0; i < len; i++ ) < if ( getStyle(all[i], style) === value ) result.push(all[i]); >return result; >
var styleEls = $$('*').filter(function(item) < return item.getStyle('position') == 'absolute'; >);
Or even create a new selector.
got me interested and so here is one ( its my 1st, so its not built for efficiency ) to find elements by css property..
$.expr[':'].css = function(obj, index, meta, stack)< var params = meta[3].split(','); return ($(obj).css(params[0]) == params[1]); >;
usage : $(‘optionalSelector:css(property,value)’)
will return all elements ( of optionalSelector ) whose property = value
example : var visibleDivs = $(‘div:css(visibility,visible)’);
will return all divs whose visibility is set to visible ( works for the default visibility as well.. )
There is no selector for CSS attributes, so you’re pretty much stuck to looping through each element and checking it’s position. Here’s a jQuery method:
$("*").each(function() < var pos = $(this).css('position'); if(pos == "absolute") < // do something >else if (pos == "relative") < // do something else >>);
You can use Case statements instead of if/else as well.
Other than this solution, there is no selector per se that can search by CSS attributes (unless they were inline, maybe).
CSS Selectors, The CSS id Selector. The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.
How to select element by order ?
In a div with class product , there are some multi-level elements. These include two images. The first image is the product thumbnail and the second image is the product rating. They both don’t have class and I can’t change the html code. Sometimes, the images is wrapped inside a tag.
I have to select the image product only. It’s the first img that appears by order inside each div.product .
img