Facing Issue on Chain Targeting on jQuery

Can you please take a look at this demo and let me know why I am not able to target the id of Next and Previous section of selected a anchor link?

to Get the Next I tried all

 var target = $(this).next('section').attr('id');
 var target = $(this).parent().parent().next('section').attr('id');
 var target = $(this).parent().next('section').attr('id');
 var target = $(this).parent().find('section').attr('id');

but was not able to grab the id of Next Section to the selected .next-slide

$(function(){
  $(".next-slide").on('click', function(event) {
      event.preventDefault();
        var target = $(this).next('section').attr('id');
      target = "#"+target;
       console.log(target); 
      $('html, body').animate({
        scrollTop: $(target).offset().top
      }, 800, function(){
      });
  });
});

Answers 1

  • You're missing in period in next('.section'). If you want to select the adjacent sibling with the class "section", use .next('.section').

    Okay, working here!

    $(function(){
      $(".next-slide").on('click', function(event) {
          event.preventDefault();
            var target = $(this).parents('section').next('section').attr('id');
          target = "#"+target;
           console.log(target); 
          $('html, body').animate({
            scrollTop: $(target).offset().top
          }, 800, function(){
          });
      });
    });
    

Related Articles