0

I have the following jQuery code which all works fine:

         $("#sidebar .m3, #sidebar .m5").toggle(function() {                  
               $(this).find("img").attr({­src:"images/Up.png"});                 
               }, function() {
               $(this).find("img").attr({­src:"images/Down.png"});                  
         }); 

My question is, instead of individually specifying $("#sidebar .m3, #sidebar .m5"), where my menu items may have the classes m1,m2,m3,m4,m5 etc, is there anyway of having something like:

$("#sidebar .m[*]").

That will cater for any number after the ".m" , so I don't need to hard code m3 or m5?

Thanks.

tonyf
  • 34,479
  • 49
  • 157
  • 246

4 Answers4

5

If that's the only (or first) class applied to the menu item you can use the attribute starts with selector.

$('#sidebar [class^=m]')...

A better way would be to give them all a common class, like menu-item, in addition to your other class that functions simply as a selector to group them all, then simply use:

$('#sidebar .menu-item')...
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • @Nick -- that's the first sentence in the answer. – tvanfosson Jul 08 '10 at 00:39
  • Hahaha, had my screen scrolled where that line was just out of view, removing that ignorant comment :) – Nick Craver Jul 08 '10 at 00:42