jquery selecting any first p element after an heading whether another element is also there
I am attempting to change the first p element that comes after an h2 or h3 element, whether or not there is another element in between.
So the html for this is
and some jquery (2.13)
So this finds the first p after an h3 or h3 tag, but it doesn’t find it if there is another element in between. Not always but there will be times when I’d want an img, div, or figure element to come after and h2 or h3, but still want jquery to find that first p (in the above code, it fails)
I have tried a number of combinations of selectors and methods including p:first-of-type, siblings, nextAll, etc. I would like to do this without adding a class to the first p element in the html portion.
It’s not working as expected because
.next('p') will only select the immediately following
p element. You could use the
.nextAll() method to select all the following
p elements (not just the immediately following one), then chain the
.first() method to get the first match in the collection: