nirmesh
nirmesh

Reputation: 121

Get id of a form belong to div containing active class

Lets say I have two divs

<div role="tabpanel" class="tab-pane fade">
    <form id="own-connection"></form>
</div>
<div role="tabpanel" class="tab-pane fade active in">
    <form id="inherited-connection"></form>
</div>

All I want is get id of child form whichever div have active as class. how could i do that? consider above two div as tab and when i click one of them active is get added as class.

Upvotes: 2

Views: 231

Answers (3)

Munkhdelger Tumenbayar
Munkhdelger Tumenbayar

Reputation: 1874

function getID(){
let $id = $(".tab-pane.active").children().attr('id');
-----------------------------------^ case like this children much better than find cause find will search thru entire dom but children only 1 lvl down
return $id;
}
usage: getID() --> will return active class children id

Upvotes: 0

Kaleem Nalband
Kaleem Nalband

Reputation: 697

check the below code,

$(function(){
var active_id=$(".tab-pane.active").find("form").attr("id");
console.log("active_form_id :"+active_id);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div role="tabpanel" class="tab-pane fade">
    <form id="own-connection"></form>
</div>
<div role="tabpanel" class="tab-pane fade active in">
    <form id="inherited-connection"></form>
</div>

hope it helps

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82231

You can use

1) Chaining of class selector to get element with class tab-pane and tabpanel

2) .Find() selector to find form element

3) .attr() to get attribute ID

 $('.tab-pane.active').find('form').attr('id')

Upvotes: 2

Related Questions