1

Помогите чтоб при клике на Открыть текст менялся на Cвернуть

https://jsfiddle.net/kcvhoaj7/

$('.btn_toggle').click(function() {
  $(this).parent().parent().find(".list-items__lvl2").slideToggle(400, function() { 
    if($(this).parent().hasClass("opened")) 
      $(this).parent().removeClass("opened");
    else $(this).parent().addClass("opened");
  });
});

2 ответа 2

1
$('.btn_toggle').click(function() {
  var $lvl2 = $(this).closest(".list-items__single").find(".list-items__lvl2");
  $lvl2.slideToggle(400, function() { 
    $(this).parent().toggleClass("opened"); 

    var $btn = $(this).parent().find(".btn_toggle");
    $btn.text($(this).parent().hasClass("opened")? "Cвернуть" : "Открыть");
  });
});
2
  • что-то вы отредактировали и перестало работать) в предыдущем варианте было все ок
    – websterjoy
    23 июн 2018 в 18:56
  • @websterjoy jsfiddle.net/kcvhoaj7/16
    – user176262
    23 июн 2018 в 18:57
1
$('.btn_toggle').click(function() {

  if($(this).html() == "Открыть")
    $(this).html("Скрыть");
  else
    $(this).html("Открыть");

  $(this).parent().parent().find(".list-items__lvl2").slideToggle(400, function() { 
    if($(this).parent().hasClass("opened")) 
      $(this).parent().removeClass("opened");
    else $(this).parent().addClass("opened");
  });
});

Может и некрасиво, зато работает)))

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.