.each () в Jquery не возвращает результат - PullRequest
0 голосов
/ 26 февраля 2020

У меня проблема с получением результата при использовании метода .each ().

вот мой код:

$(".fad-physician-profile-card-feinstein__column-wrapper").each(function(){
      const $contentId = $('.fad-physician-profile-card-feinstein__column-wrapper').attr("id");
      const $contentBody = $('#'+ $contentId + '.fad-physician-profile-card-feinstein__column-wrapper').find(".body-section__main")
      const $contentBodyArrow = $('.fad-physician-profile-card-feinstein__column-wrapper').find(".icon.icon--angle-thin")
      const $contentBodyBtn = $(".fad-btn__toggle",context)  

      if($contentBody.length >=0){
          console.log($contentId)  
          $contentBodyBtn.on('click', function toggleViewmore(e){
             $contentBodyBtn.toggleClass("btn-active");
             if($contentBodyBtn.hasClass("btn-active")){
               $contentBody.addClass("active")
               $contentBodyArrow.addClass("caret-up")
               $contentBodyBtn.text("View less")
             }else{
               $contentBody.removeClass("active")
               $contentBodyArrow.removeClass("caret-up")
               $contentBodyBtn.text("View more")
             }
          }) 
       }
  })

Фактический результат:

<div id="01" class="col-sm-9 fad-physician-profile-card-feinstein__column-wrapper">
<div id="01" class="col-sm-9 fad-physician-profile-card-feinstein__column-wrapper">

Желаемый результат:

<div id="01" class="col-sm-9 fad-physician-profile-card-feinstein__column-wrapper">
<div id="02" class="col-sm-9 fad-physician-profile-card-feinstein__column-wrapper">

Я бы использовал индекс на бэкэнде, чтобы получить значение в атрибуте id. это также должно быть возвращено в l oop.

здесь скриншот фрагмента ниже:

...