Huma Ali Huma Ali - 2 years ago 72
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

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

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

Answer Source

Use .attr() as shown :-

var id = $('').attr('id')


var id = $('').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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download