0

I have tried several ways to loop through a set of 4 li elements, each with a class of "item_n" where in is an integer 1-4. I need to increment by 1 with each click. I have come close, but it ended up getting worse. Here is my semi-working code:

$('input.compare').change(function(){
    var me = $(this).attr("refid");
    $('li.item_1').append('<img src="images/submenu/' + me + '.png" alt="compare1" height="28" width="28" />');
});

I need to loop through the li.item_1 selector like $('li.item_"+i"') with each click until four is reached.

Thanks in advance.

2 Answers 2

1

I think you could try:

$('input.compare').change(function(){
    var me = $(this).attr("refid");
    $('li [class^=item_]').each(function(){
         var me = $(this).name();
         $(this).append('<img src="images/submenu/' + me + '.png" alt="compare1" height="28" width="28" />');
        });
});

where [class^=item_] will get any class that li has and start with item_

Sign up to request clarification or add additional context in comments.

1 Comment

I'm sorry, I guess I didn't explain well enough. I have 4 li elements, like the following: <ul> <li class="item_1"></li> <li class="item_2"></li> <li class="item_3"></li> <li class="item_4"></li> </ul> When the user clicks any one of the input.compare elements, I need to append the first li[class='item_1'] with an image. When the user clicks any one of the remaining input.compare elements, it appends li[class='item_2'], and so on until the final li[class='item_4'] is appended. After the last one is appended I need to display an alert.
0

Something like this will allow you to enumerate if you give them all the same class.

$('input.compare').change(function(){
   $(".itemclass").each(function(){
     // do what you need
     alert($(this).name())
   });
 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.