@Evtera

Почему эти две функции мешают друг другу?

Нужна функция, которая бы подсвечивала кнопку "удалить" при выборе хотя бы 1 чекбокса. Всё работает корректно, но после нажатия кнопки выбрать всё при нажатии на одиночный инпут состояние кнопки "удалить" не меняется. В чем может быть причина?
https://trytopersuademe.github.io/profileAB/index.html

const checkBoxAll = () => {
  $(":checkbox").styler();

  let checked = false;
  $(".userProfile__select-All-Titles").click(function () {
    if (checked) {
      $(":checkbox").each(function () {
        $(this).prop("checked", false).trigger("refresh");
      });
      countCheckedInputs();
      checked = false;
    } else {
      $(":checkbox").each(function () {
        $(this).prop("checked", true).trigger("refresh");
      });
      checked = true;
      countCheckedInputs();
    }
    return false;
  });
};
checkBoxAll();

const countCheckedInputs = () => {
  let checkedInput = $(".checked").length;
  console.log(checkedInput);

  if (checkedInput > 0) {
    $(".userProfile__delBTN").addClass("userProfile__delBTN_active");
  } else {
    $(".userProfile__delBTN").removeClass("userProfile__delBTN_active");
  }
};

$(".jq-checkbox").on("click", function () {
  countCheckedInputs();
});
  • Вопрос задан
  • 50 просмотров
Решения вопроса 1
@aftar
const checkBoxAll = () => {
  $(":checkbox").styler();

  let checked = false;
  $(".userProfile__select-All-Titles").click(function () {
    if (checked) {
      countCheckedInputs();
      checked = false;
    } else {
      $(":checkbox").each(function () {
        $(this).prop("checked", true).trigger("refresh");
      });
      checked = true;
      countCheckedInputs();
    }
    return false;
  });
  
  $(".userProfile__delBTN").click(function () {
    $(":checkbox").each(function () {
       $(this).prop("checked", false).trigger("refresh");
      });
    checked = false;
    countCheckedInputs();
  });
};
checkBoxAll();

const countCheckedInputs = () => {
  let checkedInput = $(".checked").length;
  console.log(checkedInput);

  if (checkedInput > 0) {
    $(".userProfile__delBTN").addClass("userProfile__delBTN_active");
  } else {
    $(".userProfile__delBTN").removeClass("userProfile__delBTN_active");
  }
};

$(".jq-checkbox").on("click", function () {
  countCheckedInputs();
});
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы