Prakash Murthy
Prakash Murthy

Reputation: 13067

CSS selectors for excluding by attribute presence

The document I am parsing has the following two styles of td tags (among other instances of td tags):

<td align="right" nowrap bgcolor="#A1C87A">...</td>

<td align="right" nowrap>...</td>

How do I write a selector which only selects the second type, and exclude all the other td tags?

document.css('td:not([bgcolor="#A1C87A"])')

excludes the first type, includes the second type plus all other td tags as well.

document.css('td[align="right"][nowrap]')

excludes all other td tags, but includes both types above.

Upvotes: 9

Views: 5825

Answers (3)

matt
matt

Reputation: 79743

You can simply combine the :not selector with the other attribute selectors:

document.css('td:not([bgcolor="#A1C87A"])[align="right"][nowrap]')

You could even put the :not after the others (it doesn’t need to be right next to the element name):

document.css('td[align="right"][nowrap]:not([bgcolor="#A1C87A"])')

These will both select all td elements that have bgcolor="#A1C87A" and nowrap but don’t have align="right", which is what you’re after.

Upvotes: 14

Waxi
Waxi

Reputation: 1651

td[nowrap][bgcolor] ~ td[nowrap] { code };

Upvotes: 0

sodawillow
sodawillow

Reputation: 13176

td:not([align="right"][nowrap][bgcolor]) should be enough for what you want

Upvotes: 0

Related Questions