Skip to content Skip to sidebar Skip to footer

Jquery: How To Find An Element Which Is Coming 2 Elements Before Current Element

i have a markup which look like this:

Paragraf3-dummytext

Quisque id odio. Praesent venenatis metus at tortor pulvinar var

Solution 1:

You can do:

paragrafheading.push($(this).parent().prev().text());

If there's not always paragraph around the a, or you don't know how many parents the anchor can have before the h3, you can do something like this:

paragrafheading.push($(this).closest('> h3').find('> h3').text());

Solution 2:


Solution 3:

paragrafheading.push($(this).parents('p')[0].prevAll('h3')[0].text());

Doing it this way means that even if the structure changes in the future you will still be able to get the first ancestor h3 element regardles of how many paragraphs etc are in the dom tree before the a tag


Post a Comment for "Jquery: How To Find An Element Which Is Coming 2 Elements Before Current Element"