q0987
q0987

Reputation: 35992

jQuery - How to select the first parent of an element and add a new class "testclass"

First Name

Given element 'element_1', how to select its first parent with tag name as li?

Thank you

Upvotes: 2

Views: 3642

Answers (2)

jAndy
jAndy

Reputation: 236102

$('#element_1').closest('li').addClass('testclass');

assuming you're using jQuery 1.3+ and element_1 is an ID.

jQuery < 1.3 should look like

$('#element_1').parents('li').addClass('testclass');

Reference: .closest(), .parents()

Upvotes: 3

tvanfosson
tvanfosson

Reputation: 532595

$('#element_1').closest('li').addClass('testclass');

Upvotes: 5

Related Questions