0

I have 2 functions that triggers when an element is clicked. However, i want to use the first function that should be executed in another function via click event. I am still a newbie.

I already tried calling the function and declaring the function name inside the other function where i want it to be executed. Is it not working because the first function can only be triggered vie click event on its element? I also nested the click event on a function but it didn't work as well.

$(document).ready(function()
function toggleMenuClick(){

$('.menu-link').click(function (normal){
    normal.preventDefault();
    $('.menu-link').toggleClass('close');
    $('.overlay-menu').toggleClass('active');
    $('.menu-link').toggleClass('fixed');
    $('nav ul').toggleClass('show');
    $('nav ul li').toggleClass('fade-in');
    $('body').toggleClass('min-height');

    if ($(window).scrollTop() <= 99){
        $('.menu-link').toggleClass('white');
    }
});

}

toggleMenuClick();

$('.scroll-link').click(function(navigate){
   navigate.preventDefault();
      $("html, body").animate({
      scrollTop: $(this.hash).offset().top
      }, 700);
toggleMenuClick();
});
});

I want all the classes from toggleMenuClick to be toggled when .scroll-link is clicked as well.

3
  • 1
    Do not create event listeners in other event listeners. This leads to duplicate event listener creations, and the chances of creating conflicts with the logic stepping on itself is very high. Best case scenario you will be performing the same work multiple times. Worst case scenario, it stops working. Commented Apr 12, 2019 at 21:46
  • 1
    Actually worst case scenario is it works some times and not others, and then it's a debugging nightmare to figure out why. Commented Apr 12, 2019 at 21:48
  • It was the last resort that I thought of but apparently was logically wrong. Commented Apr 12, 2019 at 21:50

1 Answer 1

2

You need to make toggleMenuClick a separate function, outside your click handlers, so that you can call it independently.

It would look something like this:

var toggleMenuClick = function() {
    // Do classes stuff...
}


$('.menu-link').click(function(e) {
    e.preventDefault();
    toggleMenuClick();
});

$('.scroll-link').click(function(e){
    // Do stuff...

    // Now you can call your function
    toggleMenuClick();
});


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

2 Comments

thanks for this input. can i just name the function and call it instead of storing it in a variable?
Yup, you can do that like function toggleMenuClick() {. The only difference is if you do it that way, it's accessible before it's declared, via the magic of hoisting. Depends what you want.

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.