0

I have a variable (div element) which contains some table html.

I can use this javascript to add a class to each cell that has a background set.

  var tds = tempDiv.getElementsByTagName("TD");
  for (var j = 0; j < tds.length; j++) {
    var oTd = tds[j];

    if (oTd.style.background.length > 0) {
      oTd.className = 'faketh';
      oTd.setAttribute('style', 'Clear');
    } //if
  }//for

what i'd like to do is do the same in jquery. Below is what i've come up with, and the second line works fine, but the first doesn't....

  $(tempDiv).find("td[style*='background:']").addClass("faketh");
  $(tempDiv).find("td").removeAttr('style');

Any help would be appreciated.

Edit: Just to add; I'm using the code below without issue.

  $(tempDiv).find("tr:odd").addClass('row0');
  $(tempDiv).find("tr:even").addClass("row1");

So its not the adding of the class thats the problem... The issue is that i'm not finding any matching elements. Here is one of the td elements;

<td valign="top" class="faketd" style="border: 1pt solid windowtext; padding: 0cm 5.4pt; background: silver none repeat scroll 0% 0%; width: 131.4pt; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">
        <p style="margin: 0cm 0cm 0pt; text-align: justify;"><strong>VPN Name/Description:</strong></p>
        </td>
1
  • Do you know what the background is set to or just looking for any value? Commented Sep 3, 2009 at 15:31

5 Answers 5

3

I don't think you can do this using selectors natively in jQuery. The style attribute is not stored as a string by the browser, it's an object.

It has been implemented, however: http://code.google.com/p/aost/wiki/CustomJQuerySelectorInTellurium#:styles

Or you can use $.each in something like this:

$("img").each(function() {
    if($(this).css('background').length > 0) {
        $(this).addClass('faketh');
    }

});

Or you can use the jQuery filter:

var x = $("#tempDiv td").filter(function(i){
    return $(this).css("background").length > 0;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Haven't tested this yet... but its the best answer on here... hopefully will work ;)
2

try this

$(tempDiv).find("td[style*=background]").addClass("faketh");

EDIT to prevent selection of elements that have some kind of "background-" you could also do following

$(tempDiv).find("td[style*=background]:not(td[style*=background-])").addClass("faketh");

but if an element has both "background:blabla" and "background-color:#FFF", it won't be selected

3 Comments

Have double checked... an no. Removing the single quotes does not make a difference. :(
won't removing the : mean that i'll now match attributes such as background-color etc etc. I'm just after the background attribute.
Removed the colon and.... nope. :( So weird that it doesn't work... looks right to me.
1

A few warnings:

  • The contents of the style selector should be quoted (check the samples in the Jquery docs), as you have them in the question, not unquoted as others recommend.
  • Be careful with checking the style attribute in jQuery attribute selectors. The browser may modify the contents of the string (re-ordering, the spacing around the colon, etc.) for it's internal representation, and each browser does this slightly differently.

The most important bit:

  • Are you using Firefox? I've had trouble with attribute selectors in Firefox once or twice, so if you've only tested in Firefox, check Chrome/IE/Safari/Opera/etc. It won't solve the problem, but may give you a different scope for it.

Comments

0

Depending on what tempDiv is, you should also be able to shorten it up by doing:

$(tempDiv + " td[style*=background:]").addClass("faketh");

Comments

0

You are searching for the string 'background:' but in the example you gave, it's using 'background-color:'

So change it to either:

$(tempDiv).find("td[style*=background-color:]").addClass("faketh");

or:

$(tempDiv).find("td[style*=background]").addClass("faketh");

1 Comment

You are quite correct, my example td html element was incorrect. I have ammended it. Some of the td elements do have a background-color attribute, which i don't care about. I only want to match on background.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.