user1564732
user1564732

Reputation: 57

applying class to parent if child has it using jQuery

How can I use jQuery when I have this HTML code I need to add the class name to the parent li only when the child has the class name high.

thanks.

<li class="emergency" style="background-color: #FE9D9D;">
<span class="high">250</span></li>

Upvotes: 0

Views: 68

Answers (5)

lordkain
lordkain

Reputation: 3109

$(".high").parent().addClass('sss');

jsfiddle http://jsfiddle.net/M5gNA/

Upvotes: 0

devnull69
devnull69

Reputation: 16544

If I understand the question correctly, you want to add the class of the span also to the parent li, right?

$('li .high').closest('li').addClass('high');

Upvotes: 0

Felix
Felix

Reputation: 38102

You can use has():

$('li:has("span.high")').addClass('newClass');

Fiddle Demo

Upvotes: 0

Use .has()

$('li:has("span.high")').addClass('className');

li has span element with class high addCass className to it.

.addclass()

Fiddle Demo

Upvotes: 1

Anton
Anton

Reputation: 32581

You can target .high class like this then use closest() to get the parent li.

$('li .high').closest('li').addClass('myClass');

Upvotes: 2

Related Questions