Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery - Select the <li> around <a href="specific-url">

This should be simple:

<li>
    <a href=""></a>
</li>
<li>
    <a href="specific-url"></a>
</li>

I want to select that <li> based on the <a> tag's attribute with specific url in the href. (Do not use last-child)

How can I?

like image 255
J0NNY ZER0 Avatar asked Dec 06 '22 14:12

J0NNY ZER0


2 Answers

As simple as:

$('a[href="specific-url"]').parent('li');
like image 78
zerkms Avatar answered Dec 27 '22 10:12

zerkms


Here you go:

$('li:has(a[href="specific-url"])')

Live DEMO

Another way is:

$('li > a[href="specific-url"]').parent()

Live DEMO

like image 24
gdoron is supporting Monica Avatar answered Dec 27 '22 09:12

gdoron is supporting Monica