jquery closest
$( "li.item-a" )
.closest( "ul" )
.css( "background-color", "red" );
jquery closest
$( "li.item-a" )
.closest( "ul" )
.css( "background-color", "red" );
jquery nearest
$( "li.item-a" )
.closest( "ul" )
.css( "background-color", "red" );
closest jquery
Closest
<div class="product">
<div class="product-counter">
<div class="input-group-append">
<div class="input-group-append">
<button class="btn btn-outline-primary product-minus" type="button"> <i class="fas fa-minus"></i> </button>
</div>
<input type="text" class="form-control product-number" placeholder="1" value="1">
<div class="input-group-append">
<button class="btn btn-outline-primary product-plus" type="button"> <i class="fas fa-plus"></i> </button>
</div>
</div>
</div>
<button class="btn btn-light product-order"> Order + </button>
</div>
------
ex: i am at .product-order
$(document).on('click', '.product-order', function(e) {
var counter = $(this).closest('div').find('.product-counter');
var number = $(this).closest('div.product').find('.product-number');
var minus = $(this).closest('div.product').find('.product-minus');
var plus = $(this).closest('div.product').find('.product-plus');
});
get closest element id jquery
$(".isActive").click(function() {
var div_id = $(this).closest('tr').find('.display_image').attr(id);
// ...
});
javascript find nearest element
// Find the number from `arr` which is closest to `n`
const closest = (arr, n) => arr.reduce((prev, curr) => Math.abs(curr - n) < Math.abs(prev - n) ? curr : prev);
// Or
const closest = (arr, n) => arr.sort((a, b) => Math.abs(a - n) - Math.abs(b - n))[0];
// Example
closest([29, 87, 8, 78, 97, 20, 75, 33, 24, 17], 50); // 33
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us