Forum Moderators: open

Message Too Old, No Replies

add wildcard to className matching

         

benihana

10:32 am on Feb 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a stripey table row script based on the one from
a-list-apart.

Ive tweaked it a little so it is on by default, and to switch it off, i give the table a class of 'noruler'.

This is fine when thats the only class on that table, but some tables have stuff like:


class="noruler col1 col2"

I need to modify the following line to account for the multiple classes:


if(tables[i].className!=='noruler')

So what do i add to match 'noruler' followed by zero or more other class names?

Thanks

Bernard Marx

10:59 am on Feb 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use a regexp with the word boundary sequence, \b
then use the test method (returns true¦false)
This one is case-sensitive:

if(/\bnoruler\b/.test(tables[i].className))

a generic function:


function hasClass(elm, className)
{
return (new RegExp("\\b"+className+"\\b")).test(elm.className);
}

followed by zero or more other class names?

Note that both the above will return true if the className exists (as a single word) anywhere in the whole class attribute string. It is possible to modify it to make sure that the className is the very first in the string, but I doubt that really matters to you.

benihana

11:04 am on Feb 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



excellent. thanks!

<added> just realised my original post was a little misleading - should have read to NOT match</added>

Bernard Marx

11:13 am on Feb 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, it works both ways :)