Difference between nextAll() and siblings() in jQuery

1k views Asked by At

nextAll() in jQuery gets all the siblings of each element in the set of matched elements optionally filtered by a selector whereas siblings() Get the siblings of each element in the set of matched elements, optionally filtered by a selector. I want to know the difference between both of these

1

There are 1 answers

0
Bibberty On

Just a demo of what CertainPerformance states.

// Siblings - Gets everyone living in the same container at same level
console.log('Siblings');
$('#mid').siblings().each(function() {
  console.log($(this).text());
});

console.log('nextAll');
$('#mid').nextAll().each(function() {
  console.log($(this).text());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <p>1</p>
  <p>2</p>
  <p>3</p>
  <p id='mid'>4</p>
  <p>5</p>
  <p>6</p>
  <p>7</p>
  <p>8</p>
  <p>9</p>
</div>