Getting id from div class name in jQuery
this is my html code
<div class="span12" id="test" style="width: 810px;"> <ul class="nav nav-tabs"> <li class="active"><a href="/#tab1" data-toggle="tab">New Stream</a></li> <li><a id="addspan" href="/#C" data-toggle="tab">+</a></li> </ul> <div class="tabbable"> <div class="tab-content" id="tabContent"> <div class="tab-pane active" id="tab1"> @Html.Partial("_NewStreamPartial",Model) </div> </div> </div> </div> this is my javascript
<script type="text/javascript"> $(function () { var count = 2; $('#addspan').click(function () { var Id = $('.tab-pane active').attr('id'); }); }); </script> I want to get Div Id whoes class name ".tab-pane active" (means I want to get active Div Id), how can I do this?
+7
Shivkumar
source share5 answers
You can use dot to join classes in the selector
Edit
var Id = $('.tab-pane active').attr('id'); For
var Id = $('.tab-pane.active').attr('id'); +13
Adil
source shareIt should be like this:
var Id = $('.tab-pane.active').attr('id'); Here you are using the Descendant Selector . but in your house both classes are in one element. so you need to choose classes .
+2
Chamika sandamal
source shareYou can try this
$('.tab-pane active').click(function () { var Id = $(this).attr('id'); }); Hope this helps
0
Roger
source shareTry the following:
$('#addspan').click(function () { alert($('.tab-pane .active').attr('id')); }); 0
ravisolanki07
source share