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

Answers


$('#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()


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


Need Your Help

Asynchronuous call to GnomeKeyring using GObjectIntrospection

introspection gobject gnome-shell-extensions gjs gobject-introspection

I am writing a gnome-shell extension that shows the current balance of prepaid-cards like phones (or electricity). As this needs credentials for the given services, I do not want to store the passw...

Combine queries in Lucene with BooleanQuery

java solr lucene

I have the created a sample Lucene code snippet which indexes a small file. I am able to perform the indexing correctly and search on a single field values. But, I want to query on multiple fields....