1

I need to run 2 functions upon one click event and can't figure out how to nest them.

Currently both work if run separately but not together.

This function closes my menu after click.

$(document).on('click','.navbar-collapse.in',function(e) {
  if( $(e.target).is('a') ) {
    $(this).collapse('toggle');     
  }
});

And this function scrolls to my specific anchors.

function scrollToAnchor() {
  if($(".jquery-anchor").length > 0 && document.URL.indexOf("#") >= 0){  
    var anchor = document.URL.split("#")[1];
    $(".jquery-anchor").each(function() {
      if($(this).attr("name") == anchor) {
        $("html,body").animate({
          scrollTop: $(this).offset().top - 50},
          'slow');
      }
    });     
  }
}


$(function() {
  $("a[href*='#JDG']:not([href='#'])").click(function() { 
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
      && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top - 30 //offsets for fixed header
        }, 1000);
        return false;
      }
    }
  });

  //Executed on page load with URL containing an anchor tag.
  if($(location.href.split("#")[1])) {
    var target = $('#'+location.href.split("#")[1]);
    if (target.length) {
      $('html,body').animate({
        scrollTop: target.offset().top - 30 //offset height of header here too.
      }, 1000);
      return false;
    }
  }
});

I can't figure out how and where to place the top function within the click event below. This is possible right?

$("a[href*='#JDG']:not([href='#'])").click(function() { 
});

1 Answer 1

5

Remove return false from both functions

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

2 Comments

+1 You want to explain why for the benefit of others @JasperSeinhorst?
return false will prevent other handlers to execute

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.