Let us suppose I have this markup
First Hi Hello Second
Hi
Hello
Here is a fiddle
$('p').each(function() { previousH3 = $(this).prevAll('h3'); nextH3 = $(this).nextAll('h3'); if (previousH3.length > 0 && nextH3.length > 0) { $(this).css('color', 'red') } });