1$( "li.item-a" )
2 .closest( "ul" )
3 .css( "background-color", "red" );
4
1Closest
2
3<div class="product">
4 <div class="product-counter">
5 <div class="input-group-append">
6
7 <div class="input-group-append">
8 <button class="btn btn-outline-primary product-minus" type="button"> <i class="fas fa-minus"></i> </button>
9 </div>
10
11 <input type="text" class="form-control product-number" placeholder="1" value="1">
12
13 <div class="input-group-append">
14 <button class="btn btn-outline-primary product-plus" type="button"> <i class="fas fa-plus"></i> </button>
15 </div>
16 </div>
17 </div>
18 <button class="btn btn-light product-order"> Order + </button>
19</div>
20
21------
22ex: i am at .product-order
23
24$(document).on('click', '.product-order', function(e) {
25
26 var counter = $(this).closest('div').find('.product-counter');
27 var number = $(this).closest('div.product').find('.product-number');
28 var minus = $(this).closest('div.product').find('.product-minus');
29 var plus = $(this).closest('div.product').find('.product-plus');
30
31});
32
1$(".isActive").click(function() {
2 var div_id = $(this).closest('tr').find('.display_image').attr(id);
3 // ...
4});
5
1// Find the number from `arr` which is closest to `n`
2const closest = (arr, n) => arr.reduce((prev, curr) => Math.abs(curr - n) < Math.abs(prev - n) ? curr : prev);
3
4// Or
5const closest = (arr, n) => arr.sort((a, b) => Math.abs(a - n) - Math.abs(b - n))[0];
6
7// Example
8closest([29, 87, 8, 78, 97, 20, 75, 33, 24, 17], 50); // 33