Reputation: 1809
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
Reputation: 4832
Try the below code.
var id=$('.tab-pane.active').attr('id')
Upvotes: 0
Reputation: 8101
You can use attr()
function to retrieve any attribute of an element.
var id = $(".tab-pane").attr("id");
Upvotes: 0
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