Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select all content between two of the same tags?

Tags:

html

jquery

A similar question was asked before, but I'm looking for a jQuery solution using only the html below (ie. no classes or id attributes):

<h2>Foo</h2>
<p>asdf</p>
<ul><li>asdf</li></ul>
<p>asdf</p>
<h2>Bar</h2>
<p>asdf</p>
<p>asdf</p>
<h2>Baz</h2>
<p>asdf</p>
<p>asdf</p>

I'd like to use some jQuery like:

$('h2').afterButBeforeNext('h2').doSomething();

This should:

  • select all the sibling elements after the specified element, but before the next occurrence of the specified element.
  • if there is no end element, then select all the elements underneath it.
like image 339
Beau Smith Avatar asked Dec 18 '25 22:12

Beau Smith


1 Answers

The prev selector should be able to do this: http://docs.jquery.com/Selectors/siblings#prevsiblings

$("h2 ~ *:not(h2)").doSomething();

You still need some sort of id or attribute to select just one single h2 element.

like image 107
markmywords Avatar answered Dec 20 '25 16:12

markmywords



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!