Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Which jquery method should I use to get an "associated" higher up element?

I’m trying to get the text value of ‘.discountData’ from this structure:

<div class="cart-item--content cl-plate-cart-item">
  <div class="discountData" hidden=""> blah blah, bo bo , jo jo, </div>
  <div class="breathe-space" style="display: flex;">
    <div class="flex-child"> QTY: <span style="font-weight: bold;"> 4 </span></div>
  </div>
  <div> DETAILS: </div>
  <div> back: <div class="breathe-space"> Text: <span style="font-weight: bold;">TEST</span></div>
    <div class="flex-container" style="padding: 5px;">
      <div style="flex: 2 1 0%;"><input class="qty-box-2" type="text" id="33461535965272" name="qty"></div>
    </div>
  </div>
</div>

I’m going from ".qty-box-2" which isn’t a direct child of ‘.discountData’
I’ve tried:

$(this).siblings('.discountData');
$(this).parent('.discountData').text();
$(this).closest('.discountData').text();

Cousin didn’t work either… That’s a bad joke. Any help?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use .closest() and then .find():

$(this).closest('.cart-item--content.cl-plate-cart-item').find('.discountData').text();

Ex:

console.log( $('.qty-box-2').closest('.cart-item--content.cl-plate-cart-item').find('.discountData').text() )
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<div class="cart-item--content cl-plate-cart-item">
  <div class="discountData" hidden=""> blah blah, bo bo , jo jo, </div>
  <div class="breathe-space" style="display: flex;">
    <div class="flex-child"> QTY: <span style="font-weight: bold;"> 4 </span></div>
  </div>
  <div> DETAILS: </div>
  <div> back: <div class="breathe-space"> Text: <span style="font-weight: bold;">TEST</span></div>
    <div class="flex-container" style="padding: 5px;">
      <div style="flex: 2 1 0%;"><input class="qty-box-2" type="text" id="33461535965272" name="qty"></div>
    </div>
  </div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading