synan54
synan54

Reputation: 658

jquery parent() select <a> in main parent

hi i am working on a project i wanna catch of a parent but i could not do it thank you for your advice

this is what is in html part

<li> <a class="siralaMain" catid="7" href="#"> <span>Sahil</span> </a>
    <ul style="display: block;">
        <li> <a class="siralaSec active" catid="300" href="#"> </li>
    </ul>
</li>

$(this) references <a class="siralaSec active" catid="300" href="#"> here

when i made

$(this).parent().parent().parent().addClass("active");

it addClass to li so it becomes like this

<li class="active"> <a class="siralaMain" catid="7" href="#"> <span>Sahil</span> </a>
    <ul style="display: block;">
        <li> <a class="siralaSec active" catid="300" href="#"> </li>
    </ul>
</li>

but i need to addClass to a like this

<a class="siralaMain active">

thanks for any advice

Upvotes: 1

Views: 99

Answers (6)

Adil Shaikh
Adil Shaikh

Reputation: 44740

How about this -

$(this).parents("li:eq(1)").find("a:first").addClass("active");

http://jsfiddle.net/mohammadAdil/4zgM2/1/

Upvotes: 0

user2308046
user2308046

Reputation:

$(this).closest('.siralaMain').addClass('active');

seem like you need to use:

 $(this).closest('ul').prev().addClass('active');

Upvotes: 2

Sujesh Arukil
Sujesh Arukil

Reputation: 2469

try this

 $(this).closest('ul').prev('.siralaMain').addClass('active');

Upvotes: 0

tymeJV
tymeJV

Reputation: 104775

This works:

$(this).parent().parent().parent().find("a").addClass("active");

Fiddle: http://jsfiddle.net/Afgnz/

Upvotes: 0

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196002

Use

$(this).closest('li:has(.siralaMain)')
       .find('.siralaMain')
       .addClass('active');

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

Ugly parent chain is ugly.

$(this).parent("li").parent("li").find("a").eq(0).addClass("active");

Upvotes: 0

Related Questions