I need to loop through some elements in the page and then, for each one, if it had a class beginning with, for example, “C”, do something.
$('#dialog li').each(function(){
if ($(this).hasClass("^C")){
//do something
}
}
It may sound silly, but what selector/method should I use on the if clause?
Carefull with
$('#dialog li[class^="C"]')! It will only match elements, whose class attribute starts with “C” not ones with a class starting with C. For example it will not match<li class="foo Clown">.AFAIK what you want is not possible mit jQuery alone. You would need to loop through the classes and check each separatly. Something like:
Alternativly you should consider changing your approach, and give all elements an additional class and filter by that. This has the addvantage that it can also be used in CSS: