3

Suppose in my style sheet:

.test{width: 20px; height: 50px; color: blue;}
.some-test{padding: 20px; margin: 10px;}
.even-test{font-size: 20px; color: red;}

Is it possible to detect the 20px value of each css properties so that I could replace it?


I'm supposing to detect something like this:

$('.selector').filter(function() {
    return $(this).css(property) === '20px';
}).css(property, value);

Isn't there anything something like: style.arguments[] ?

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231

2 Answers2

0

You can use the filter function in jQuery to find all elements that have that width.

$('.allYourSelectors').filter(function() {
    return $(this).css('width') === '20px';
}).css("width", newWidth);

However, it would be much better to just give them all the same class that you can modify.

Jivings
  • 22,834
  • 6
  • 60
  • 101
0

To search through CSS styles (not sure if this gets style="..." styles):

var parseStyle = function(rules) {
    for (var i = 0; i < rules.length; ++i)
    {
        console.log(rules[i].style.width);
    }
};

//care needs to be taken as to when this executes - styles may not have been loaded yet
for (var i = 0; i < document.styleSheets.length; ++i)
    if (document.styleSheets[i].rules && document.styleSheets[i].rules.length > 0)
        parseStyle(document.styleSheets[i].rules);

If it's not the CSS properties you want but the result of the style on an element, this might be more what you're after...

var element = ... jquery or whatever
var elementStyle = element.currentStyle || getComputedStyle(element);
console.log(elementStyle.width);
jozxyqk
  • 16,424
  • 12
  • 91
  • 180