I have a problem with jQuery. In my HTML Code I have some elements with rising numbers like this:
<ul class="flexer-0"> ... <ul class="flexer-1"> ... <ul class="flexer-2">
This classes the ul get with this code:
$('ul').addClass(function(index) {
return 'flexer-' + index;
});
How am I able to select this elements later by jQuery.
My idea was like this:
$("ul.flexer-0") ...
$("ul.flexer-1") ...
$("ul.flexer-2") ...
This is stressful, because it will be become a long page with many ul-lists. It should be possible to get the index-number with jQuery.
Has anyone an idea?
Kai
If the
flexer-nis the only class in the element’sclassattribute, you can use an attribute starts with selector to select all matches:That will match any
ulwhoseclassattribute starts withflexer.But note that it will not work if you have other classes on, for instance:
I’d take a step back and ask: Why do you have all of these
flexer-nclasses? It sounds like a single class, perhaps combined withids or perhaps adata-*attribute on the individual flexers, would make more sense.