0

I have this list browse plugin with next & prev button

HTML :

<ul>
  <li><a href="http://www.google.com/">Google</a></li>
  <li><a href="http://www.yahoo.com/">Yahoo</a></li>
  <li><a href="http://www.aol.com/">Aol</a></li>
</ul>

<input type="button" class="prev" value="< Prev" />
<input type="button" class="next" value="Next >" />

JQUERY :

(function(){
    var $lis  = $('ul li'),
        index = 0;

    function li_click(){
        lastIndex = index;
        index = $(this).index();
        $lis.eq(index).addClass('active');
        $lis.eq(lastIndex).removeClass('active');
    }

    $lis.click(function () {
        $lis.eq(index).removeClass('active');
        index = $(this).index();
        $lis.eq(index).addClass('active');
    }).eq(0).addClass('active');

    $('.next, .prev').click(function () {
        $lis.eq(index).removeClass('active');
        if ($(this).hasClass('prev')) {
            index--;
            if (index < 0) {
                index = ($lis.length - 1);
            }
        } else {
            index++;
            if (index >= $lis.length) {
                index = 0;
            }
        }
        $lis.eq(index).addClass('active');
    });
})();

Here is Fiddle

This plugin works fine. But I want to trigger its child element ('a') on clicking next/prev buttons

for example, I click on next button & move to Yahoo then I want its child ('a') to get clicked. same thing goes with prev button. I want to trigger child element ('a') of newly added parent class ('.active')

How can I achieve this ?

2 Answers 2

1

This should work:

$(".active > a").trigger("click");
Sign up to request clarification or add additional context in comments.

3 Comments

Well it does work. The reason why you don't see anything happening is because in your code, you don't have any events attached to the onclick event of the a elements. If you want to follow that link when pressing prev/next, you want to do this: window.location.href = $(".active a").attr("href");
I have added onclick event here : jsfiddle.net/Rtmdj/29 But it still not working...
No, of course your script doesn't work. There is no .active element when the script runs, so your onclick event will not attach to anything. Here, try this one: jsfiddle.net/Rtmdj/30
0

I think this should do it:

$lis.eq(index).addClass('active').find('a').trigger('click');

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.