jQuery: Selecting immediate siblings
jquery
Solution
Using .text(), .prevUntil() and .nextUntil()
To get text of all previous and next `.MyClass` element from clicked `.MyBorder`:
$('#Wrapper').on('click', '.MyBorder', function() {
var AddressString = [];
$(this).nextUntil('.MyBorder').text(function(index, text) {
AddressString.push(text);
});
$(this).prevUntil('.MyBorder').text(function(index, text) {
AddressString.push(text);
});
console.log(AddressString.join(', '));
});
Combination of `prevUntil()`, `nextUntil()` with siblings()
$('#Wrapper').on('click', '.MyClass' function() {
var AddressString = [];
$(this).siblings('.MyClass').prevUntil('.MyBorder').add($(this).prevUntil('.MyBorder')).text(function(index, text) {
AddressString.push(text);
});
console.log(AddressString.join(', '));
});
Problem
Suppose I have this HTML: ``` <div id="Wrapper"> <div class="MyClass">some text</div> <div class="MyClass">some text</div> <div class="MyBorder"></div> <div class="MyClass">some text</div> <div class="MyClass">some text</div> <div class="MyClass">some text</div> <div class="MyBorder"></div> <div class="MyClass">some text</div> <div class="MyClass">some text</div> </div> ``` I want to get the text of the MyClass divs next to the one clicked on, in the order they're in. This is what I have: ``` $('#Wrapper').find('.MyClass').each(function () { AddressString = AddressString + "+" + $(this).text(); }); ``` I know adds ALL the MyClass divs; I'm just wondering if there's a quick way to do it. Thanks.