Short answer:

$(".button").each(function(){
    var icon = /icon(\w+)/.exec(this.className)[1].toLowerCase();
   $(this).append('<img src="images/' + icon + '.gif" />');
});

- ricardo

On Apr 16, 9:58 am, jonhobbs <jon.hobbs.sm...@gmail.com> wrote:
> This might be hard to explain, but I need a way to loop through a
> bunch of elements I've already selected and for each one find classes
> that start with the word "icon". So for example I might have the
> following elements
>
> <div class="button iconStar"></div>
> <div class="button iconPlus"></div>
> <div class="button iconLeft"></div>
> <div class="button iconRight"></div>
> <div class="button iconUp"></div>
> <div class="button iconDown"></div>
>
> So, I begin by selecting the elements and looping through them....
>
> $(".button").each(function(){
>     // Some code here
>
> });
>
> Now, I could put the following code in the loop...
>
> if ($(this).hasClass("iconStar")){
>     $(this).append("<IMG SRC='Images/star.gif'>");
>
> }
>
> I would then have to repeat that for each possible icon, which seems
> very inefficient.
>
> What I'd like to do in the "each" loop is just cycle through all the
> classes that $(this) has and pick out the one that begins with ICON
> and then use that to append the image.
>
> Can anyone help?

Reply via email to