4

Hello – My question is best summarize with the intended output and the real output. Any clue why it's doing this, using the following HTML and JS code?

HTML Code:

<h3>CATEGORY 1</h3>
<p>Item 1</p>
<p>Item 2</p>

<h3>CATEGORY 2</h3>
<p>Item 3</p>
<p>Item 4</p>

<h3>CATEGORY 3</h3>
<p>Item 5</p>
<p>Item 6</p>

JavaScript / jQuery Code:

$(".h3").each(function () {

  // Display H3 Text
  console.log($(this).text());

  $(this).siblings('p').each(function () {
    if ( $(this).next().is('h3') ) {

      // Display Last Paragraph Text Before <H3>
      console.log($(this).text());

      // Break the Each Loop, Go to next H3
      return false;
    }
    else {

      // Display Paragraph Text
      console.log($(this).text());
    }
  });
});

Intended Output:

CATEGORY 1
Item 1
Item 2
CATEGORY 2
Item 3
Item 4
CATEGORY 3
Item 5
Item 6

Real (Unintended) Output:

CATEGORY 1
Item 1
Item 2
CATEGORY 2
Item 1
Item 2
CATEGORY 3
Item 1
Item 2

Thanks.

3
  • I'm confused why you worry about checking for the next h3. If the <p> tag is within the <h3>, the nested loop will automatically handle going to the next <h3> when it reaches the end of the <p> selector. Commented Jul 14, 2010 at 18:20
  • 1
    @Jason: The p elements are not inside the h3 elements. They are not even allowed there. Commented Jul 14, 2010 at 18:33
  • @Felix - Wow. I'm a noob. Haha, I even knew that and still asked the dumb question anyway. Thanks for the clarification (and for not mowing me over). Commented Jul 14, 2010 at 20:33

4 Answers 4

4

Because siblings() selects all siblings, all previous and all following. I think you need nextAll():

Get all following siblings of each element in the set of matched elements, optionally filtered by a selector.


Demo

$("h3").each(function () {

  // Display H3 Text
  console.log($(this).text());

  $(this).nextAll('p').each(function () {
    if ( $(this).next().is('h3') ) {

      // Display Last Paragraph Text Before <H3>
      console.log($(this).text());

      // Break the Each Loop, Go to next H3
      return false;
    }
    else {

      // Display Paragraph Text
      console.log($(this).text());
    }
  });
});

gives:

CATEGORY 1
Item 1
Item 2

CATEGORY 2
Item 3
Item 4

CATEGORY 3
Item 5
Item 6
Sign up to request clarification or add additional context in comments.

Comments

2

If there are no more siblings after the last <p> element, I guess I'd use .nextUntil('h3') instead:

$("h3").each(function() {
  console.log($(this).text());
  $(this).nextUntil('h3').each(function() {
     console.log($(this).text());
  });
});

If you wanted, you could even do it without the explicit calls to .each()

$("h3").text(function(i,txt) {
  console.log(txt);
  $(this).nextUntil('h3').text(function(i,txt) {
    console.log(txt);
  });
});

Comments

1

The .siblings() function does not mean "subsequent siblings", it means "all siblings." The first two <p> tags are siblings of all the <h3> tags.

Comments

1

Try using nextAll() instead of siblings().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.