Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all elements that have a specific CSS, using jQuery

How can I select all elements that have a specific CSS property applied, using jQuery? For example:

.Title {     color:red;     rounded:true; }  .Caption {     color:black;     rounded:true; } 

How to select by property named "rounded"?

CSS class name is very flexible.

$(".Title").corner(); $(".Caption").corner(); 

How to replace this two operation to one operation. Maybe something like this:

$(".*->rounded").corner(); 

Is there any better way to do this?

like image 270
ebattulga Avatar asked Aug 03 '09 06:08

ebattulga


People also ask

Can you use CSS selectors in jQuery for selecting elements?

Projects In JavaScript & JQueryjQuery uses CSS selector to select elements using CSS. Let us see an example to return a style property on the first matched element. The css( name ) method returns a style property on the first matched element. name − The name of the property to access.

How can you select all elements in page using jQuery?

The * selector selects all elements in the document, including html, head and body. If the * selector is used together with another element, it selects all child elements within the specified element.

What is the use of CSS () method in jQuery?

jQuery css() Method The css() method sets or returns one or more style properties for the selected elements. When used to return properties: This method returns the specified CSS property value of the FIRST matched element.


2 Answers

This is a two year old thread, but it was still useful to me so it could be useful to others, perhaps. Here's what I ended up doing:

var x = $('.myselector').filter(function () {      return this.style.some_prop == 'whatever'  }); 

not as succinct as I would like, but I have never needed something like this except now, and it's not very efficient for general use anyway, as I see it.

like image 104
Bijou Trouvaille Avatar answered Oct 07 '22 11:10

Bijou Trouvaille


Thank you, Bijou. I used your solution, but used the jQuery .css instead of pure javascript, like this:

var x = $('*').filter(function() {     return $(this).css('font-family').toLowerCase().indexOf('futura') > -1 }) 

This example would select all elements where the font-family attribute value contains "Futura".

like image 37
Ivan Chaer Avatar answered Oct 07 '22 11:10

Ivan Chaer