views:

948

answers:

2

Hi folks,

So basically I use jQuery for alternating row colours by selecting all the tr tags associated with the table to be coloured and then subsequently colouring them accordingly. There are certain times where I do not want a certain tr to be coloured, however, and in those cases the alternating colourization should skip over those particular table rows. For this purpose I have a class called "rowSkip" which i apply to all rows which the colourization should be skipped over.

For months I have had this working and it works a treat... however, there has always been one problem. There are cases when I need to apply multiple classes to a table row, but also don't want it coloured. jQuery seems to have a problem with its class selector under these circumstances - either that or I'm missing something simple here...

EG:

<tr class="rowSkip"> --> works fine.

<tr class="rowSkip strong someclass1 someclass2"> --> Does not work (still gets coloured despite the presence of the "rowSkip" class)

Does anyone have any idea why this might be and how I might get around it short of grabbing the class attr in its entirety, doing a split by the space, and then iterating through it and checking for the presence of rowSkip?

Code is below:

$("Table.alternate tr:not(.rowSkip)").each(function() {
//if(!$(this).hasClass("rowSkip")) { //Also tried this, and several other renditions to no avail.

   $(this).addClass(i++ % 2 == 0 ? 'rowOff1' : 'rowOff2');
//}
});

Thanks, Mark

+1  A: 

Try this:

$("Table.alternate tr:not('[class*=rowSkip]')").each

well if your class is always going to start with rowSkip then you can do this

$("Table.alternate tr:not('[class^=rowSkip]')").each //looks for class names that starts with rowSkip
TStamper
That just bombs the page.
Mark
I should also mention i tried:$("Table.alternate tr:not([class*=rowSkip])").eachThe result of which is the same as the original problem
Mark
@Mark- bombs the page?
TStamper
@Mark- I wanted you to include "Table.alternate" in front from the beginnging, I just left it off because I assumed you knew that, but I updated with the exact first line should be before "each" and an alternate way
TStamper
@TStamper- Yeah I figured that's what you meant but I wasn't sure, so I tried both and neither worked.It turns out though, that I was editing the wrong block of code like a complete twit. My original code had worked fine all along, only I was applying it to the wrong table/class - there was an inefficient and all together silly block of code directly underneath to apply the same effect to a different table's class.*sigh*Thank you for your help none the less :)
Mark
A: 

I would recommend you look at the jQuery selector build just for this, :odd and :even. Then you have a couple ways:

$('table.alternating tr:odd td').css('background-color','#eee');

or use a class:

$('table.alternating tr:odd').addClass('odd');

and some CSS:

tr.odd td { background-color: #eee }
ndp
Thanks for the tip. I came across that selector build a while ago.. I may change it when I get 5 mins but for now this is doing the job :)
Mark