Huma Ali
Huma Ali

Reputation: 1809

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">

Upvotes: 0

Views: 56

Answers (3)

Sudharsan Selvaraj
Sudharsan Selvaraj

Reputation: 4832

Try the below code.

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

Upvotes: 0

Dhara Parmar
Dhara Parmar

Reputation: 8101

You can use attr() function to retrieve any attribute of an element.

var id = $(".tab-pane").attr("id");

Upvotes: 0

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18893

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

Upvotes: 2

Related Questions