1

I am working on a carousel.

Whenever a slide changes, I get the index value of that slide.

There are three slides! so 0-2 are the index values.

Now I have this html on the page, for three dots, which indicate which slide is active. So if the dot is white, the slide is active.

This is simply done by adding the "act" class to the element.

<a id="th_1" class="thumb act"></a>
<a id="th_2" class="thumb"></a>
<a id="th_3" class="thumb"> </a>

Above you can see "th_1" is active. The others have all grey dots in the css.

Anyways, I have the index by this js code:

var slide = $(this).find('.slideshow.activated');
var index = slide.index();

Above, we find the element in this slide DIV, which has the classes item and active. "this" is a container div.

I just don't know how to make this into a function, which adds the "act" class to the correct html element as above.

perhaps I should change the element names to become zero-based (th_0, th_1, th_2) and use some regex?

I am wondering if anyone knows any tips on this.

Thanks

4 Answers 4

1
$('.thumb').removeClass('act').eq(index).addClass('act');
Sign up to request clarification or add additional context in comments.

Comments

1

If I understand correctly, you have the zero-based index of the active element in the slide div and you want to select the element with the same index among those that have the class thumb.

You can do this with $(".thumb").slice(index, 1), so for example to add the class act to that element:

$(".thumb").slice(index, 1).addClass('act');

If you also need to remove the class from the previously active element, you can do this conveniently with

$(".thumb").removeClass('act').slice(index, 1).addClass('act');

There is no need to change your ids with this approach (it does not even care if your elements have ids); it just requires that there is an 1-1 correspondence between elements in the slide div and thumb elements.

Comments

0

there is no need to change the indexes, you can do something like

$('a.thumb').removeClass('act');
$('#th_' + (index + 1)).addClass('act');

Comments

0

Does this helps?

var slide = $(this).find('.slideshow.activated');
var index = parseInt(slide.index()) + 1;
$('.thumb').removeClass('act');
$('#th_'+index).addClass('act');

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.