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?
>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>