2

If I have code like this inside my view page

<li>
     <a class="selected" href="/">Some link</a>
</li>

how can I use jquery to find this element (with selected class) and append to it's parent li element some css class, result should be like this

   <li class="some-class">
      <a class="selected" href="/">Some link</a>
    </li>

5 Answers 5

4

use:

$('.selected').parent().addClass('some-class')

or

$('.selected').closest('li').addClass('some-class')
Sign up to request clarification or add additional context in comments.

Comments

1

Using Jquery you can use the parent function: https://api.jquery.com/parent/

Comments

0

https://api.jquery.com/parent/

$(".selected").parent().addClass("some-class")

Comments

0

You can use .parent()

$('.selected').parent().addClass('some-class')

or .closest()

$('.selected').closest('li').addClass('some-class')

Comments

0

You can get parent by jquery parent api doc:

https://api.jquery.com/parent/

try like this:

$(".selected").parent().addClass("some-class");

or

$(".selected").parent("li").addClass("some-class");

Demo : http://jsfiddle.net/qsDn5/50/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.