A
A
Artur Lastname2019-11-22 08:32:03
Python
Artur Lastname, 2019-11-22 08:32:03

How to disable all OTHER handlers on a js/bitrix element?

In general, I hang my handler on the button to increase / decrease the number of goods, under a certain condition an alert pops up and, in fact, you need not change the number, BUT Bitrix (or maybe in general js) does not care what is returned false (event.preventDefault () /event.stopImmediatePropagation()) it still executes other handlers attached to these elements (namely, increases / decreases the number).

Well and actually a question - how to disconnect all OTHER handlers?
And it is worth noting:
The problem is precisely that these events are turned off only when my condition is reached, i.e. my handler worked, and did not let other handlers on this element work (at the moment I have an alert, but then all the other handlers that are hung go).

And if the person then entered everything normally (in this case, the number of goods), then my handler returns true, and the rest work as if nothing had happened.

For bitrixoids: I
also tried to use BX.denyEvent / BX.allowEvent, too, slowly, as well as the implementation of the handler through BX.bind

$(document).ready(function() {

    function checkInputsQuantity($elem){
        let prevQuantity = $elem.data('val'),
            totalCount = 0;

        $('.basket-item-amount-filed').each(function(){
            totalCount += Number($(this).val());
        });

        if (totalCount > 50){
            return false;
        }

        return true;
    }

    $(document).on('change','.basket-item-amount-filed', function () {
        if (!checkInputsQuantity($(this))){
            //BX.unbindAll($(this)[0]);

            alert('Невозможно заказать более 50 упаковок, при таком заказе просим обратиться напрямую к нашим специалистам по телефону +7 (342) 213-69-40');
            return false;
        }
    });

    $(document).on('click', '.basket-item-amount-btn-plus, .basket-item-amount-btn-minus', function (e) {
        if (!checkInputsQuantity($(this).parents('.basket-item-block-amount').find('input'))){
            let el = $(this)[0];
            //BX.unbindAll($(this)[0]);
            alert('Невозможно заказать более 50 упаковок, при таком заказе просим обратиться напрямую к нашим специалистам по телефону +7 (342) 213-69-40');
            //setTimeout("BX.allowEvent(el,'click');",1000);
        }
    });
});

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pashenka, 2019-11-22
@like-a-boss

In order for the handler to be removed, you need to pass a reference to the function when installing it

function f(event) {..}

$(document).on('click', f); // установили

$(document).off('click', f); // сняли

A
Artur Lastname, 2019-11-22
@Kekemeke

In general, I had to crutch.
If anyone is suddenly interested, I cloned the element, but hid the real one. When a clone is clicked, the check I need goes on, and if the condition is TRUE, then I initiate a click on the original.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question