No, thats where you seem to be mistaken :) , the requirement is to
find the number of elements in series (adjacent siblings),
At html
at Question, there are not any .large
elements which are adjacent siblings of .large
elements; as all .large
elements are descendants of span
elements. See Adjacent sibling selectors .
You can use :has()
selector span:has(.large) + span:has(.large)
to return elements that are adjacent siblings of span
that contains .large
element. Note, this should return the span:has(.large)
of the first span:has(.large)
not including the first span:has(.large)
. Though the accurate .length
can be derived by determining the total .length
of span:has(.large)
elements then, either adding one or multiplication and division.
var adjacentLarge = $("span:has(.large) + span:has(.large)");
console.log(
adjacentLarge.length, // this should return 3; .large having text 120, 300, 520
$("span:has(.large)").length
* (adjacentLarge.length / adjacentLarge.length) // 5
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<span><div class="amount large">100</div></span>
<span><div class="amount large">120</div></span>
<span><div class="amount large">300</div></span>
<span><div class="amount small">90</div></span>
<span><div class="amount large">110</div></span>
<span><div class="amount large">520</div></span>
You can use .filter()
, .prev()
, .is()
, .nextUntil()
, .nextAll()
, .add()
, .next()
var adjacentLarge = $("span:has(.large):first").filter(function() {
return !$(this).prev().is(":has(.large)")
});
var firstLargeSet = adjacentLarge.nextUntil(":not(:has(.large))")
.add("span:has(.large):first");
var secondLargeSet = firstLargeSet.filter(":eq(-1)")
.nextAll(":has(.large)")
.filter(function() {
return $(this).next().is(":has(.large)")
|| $(this).prev().is(":has(.large)")
});
function isBigEnough(element, index, array) {
return element >= 100;
}
function isNotBigEnough(element, index, array) {
return element <= 99;
}
var values1 = [];
firstLargeSet.each(function(index) {
console.log(this.textContent); // 100, 120, 300
values1.push($(this).text());
});
if (values1.every(isBigEnough)) {
console.log("Large"); // Large
} else if (values1.every(isNotBigEnough)) {
console.log("Small");
} else {
console.log("No Series")
}
var values2 = [];
secondLargeSet.each(function(index) {
console.log(this.textContent); // 110, 520
values2.push($(this).text());
});
if (values2.every(isBigEnough)) {
console.log("Large"); // Large
} else if (values2.every(isNotBigEnough)) {
console.log("Small");
} else {
console.log("No Series")
}
var adjacentLarge = $("span:has(.large):first").filter(function() {
return !$(this).prev().is(":has(.large)")
});
var firstLargeSet = adjacentLarge.nextUntil(":not(:has(.large))")
.add("span:has(.large):first");
var secondLargeSet = firstLargeSet.filter(":eq(-1)")
.nextAll(":has(.large)")
.filter(function() {
return $(this).next().is(":has(.large)")
|| $(this).prev().is(":has(.large)")
});
function isBigEnough(element, index, array) {
return element >= 100;
}
function isNotBigEnough(element, index, array) {
return element <= 99;
}
var values1 = [];
firstLargeSet.each(function(index) {
console.log(this.textContent); // 100, 120, 300
values1.push($(this).text());
});
if (values1.every(isBigEnough)) {
console.log("Large"); // Large
} else if (values1.every(isNotBigEnough)) {
console.log("Small");
} else {
console.log("No Series")
}
var values2 = [];
secondLargeSet.each(function(index) {
console.log(this.textContent); // 110, 520
values2.push($(this).text());
});
if (values2.every(isBigEnough)) {
console.log("Large"); // Large
} else if (values2.every(isNotBigEnough)) {
console.log("Small");
} else {
console.log("No Series")
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<span><div class="amount large">100</div></span>
<span><div class="amount large">120</div></span>
<span><div class="amount large">300</div></span>
<span><div class="amount small">90</div></span>
<span><div class="amount large">110</div></span>
<span><div class="amount large">520</div></span>