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:
Not sure if it's the best way, but can you just do each() on the h3's until you find the one that contains the 'a' tag that you want?
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"