Search code examples
jqueryclassif-statementwildcardindexof

JQuery if/else statement matching a wildcard CSS name


I am trying to write an if else statement in JQuery, which can change an element's class by matching 'IN' or 'OUT'.

For example: I have several <DIV> tags that each have class='IN-something' or class='OUT-something'.

The below would work if I knew the exact CSS class, but all I'll know is that it contains 'IN' or 'OUT'.

So something like this:


  if ($(jRow).hasClass('IN-*')){
    jRow.attr( "class", "OUT-foo" );
  }else{
    jRow.attr( "class", "IN-foo");
  }

Does anyone have any helpful ideas?


Solution

  • if ($(jRow).attr('class').indexOf('IN-') !== 1)
      {jRow.attr( "class", "OUT-foo" );}
    else  
      {jRow.attr( "class", "IN-foo");}