user14767761
user14767761

Reputation:

Get Parent "li" text using jquery

jQuery(".hm_filter_middle-category-container [role=listbox] li").on("click", function () {
        selected_category = jQuery(this).text().replace(/ *\([^)]*\) */g, "");
        parent_selected_category = jQuery(this).closest('ul').closest('li');
    console.log(parent_selected_category);
    })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="hm_filter_middle-category-list_wrapper">
   <li data-accordion-id="12" data-auto-select="true" class="accordionItemController">Dresses<span>(1566)</span></li>
   <div id="accordionContent_12" class="hm_filter_middle-category-list sub-category2 accordionItem">
      <ul role="listbox" tabindex="0" aria-label="folder list">
         <li data-value="Mini" data-id="12" tabindex="-1" role="option"  aria-selected="false">Mini<span>(1566)</span></li>
      </ul>
   </div>
</div>

I am trying to get parent li id of this,

Expected value on click the "Mini" in below code would be "Dresses".

Any thoughts, how to get it on click the "Mini" Li ?

code i tried added in snippet

Upvotes: 1

Views: 65

Answers (1)

noshad b.e
noshad b.e

Reputation: 124

//if i understood your question correctly you need like this


  $("[data-id]").click(function(){
          var id = $(this).parents(".hm_filter_middle-category-list_wrapper").find('.accordionItemController').attr("data-accordion-id")
        })

or

if it is not dynamic or the li is always before the accorionContent_12 like this:

   $("[data-id]").click(function(){
              var id = $(this).parents("#accordionContent_12").prev().attr("data-accordion-id")
            })

Upvotes: 1

Related Questions