Huma Ali Huma Ali - 5 months ago 8
Javascript Question

How to get an element's id from its classname

I want to know how can I get the id of an element based on a specific class name? In the following

HTML
I want to retrieve the
id
of the div having class
tab-pane active


<div class="tab-pane active" id="Basic">

Answer

Use .attr() as shown :-

var id = $('div.tab-pane.active').attr('id')

OR

var id = $('.tab-pane.active').attr('id')

Side Note :- I am assuming that you have only one element with class tab-pane active in your DOM otherwise you have to use $.each() to loop through all div with class tab-pane active