Hello. Tell me how to make a competent check on the availability of the condition.

There is a piece of code that is responsible for deleting several classes by touch event. In the result of the check ($('#popup_new_custom_kit').hasClass('popup-show')) = true It is necessary that the code does not work, the classes are not deleted

Initial code

 function closeSelectKitTouch() { $(document).on('touchstart' && 'touchend', function (e) { var container = $(".swipe-menu"); if (container.has(e.target).length === 0) { $('.wrapper ').removeClass('active-swipe-menu'); $('#main-wrap').removeClass('active-swipe-menu'); } }); } 

Changed code (everything works as it should)

 function closeSelectKitTouch() { $(document).on('touchstart' && 'touchend', function (e) { if ($('#popup_new_custom_kit').hasClass('popup-show')) { // что тут должно быть? } else { var container = $(".swipe-menu"); if (container.has(e.target).length === 0) { $('.wrapper ').removeClass('active-swipe-menu'); $('#main-wrap').removeClass('active-swipe-menu'); } } }); } 

I'm embarrassed by the fact that my actions for if are just empty.

Actually this is the question whether it is possible to leave? Or what should be there?

    1 answer 1

    Put "!" before the condition i. :

     function closeSelectKitTouch() { $(document).on('touchstart' && 'touchend', function (e) { if (!$('#popup_new_custom_kit').hasClass('popup-show')) { // т.е. "если не" var container = $(".swipe-menu"); if (container.has(e.target).length === 0) { $('.wrapper ').removeClass('active-swipe-menu'); $('#main-wrap').removeClass('active-swipe-menu'); } } }); } 

    Also pay attention to the design:

     'touchstart' && 'touchend' 

    Is it equal to true? Honestly, I'm not sure, but there is a suspicion that this will not work as expected.

    Several events are simply declared with a space:

     'touchstart touchend' 
    • Thanks, everything works - pridan