7

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 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 i can do this?

6 Answers 6

14

You can use dot to join classes in selector

Change

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

To

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

It should be like this,

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

You are using Descendant Selector here. but in your dom both classes are in same element. so you have to select by both classes.

0

You can try this

$('.tab-pane active').click(function () {
   var Id = $(this).attr('id');
});

Hope this helps

0

Try this :

 $('#addspan').click(function () {
           alert($('.tab-pane .active').attr('id'));
 });
0

You can get any HTML Attribute from an element with jQuery's .attr():

$('.tab-pane.active').attr('id');

Similarly, you can also get the JS Object Property value using .prop():

$('.tab-pane.active').prop('id');
0
0

Try the following code

$("#addspan").click(function(){console.log($(this)[0].id);});
1
  • 1
    Although your answer might be right, please try adding some more explanation to your answer
    – Ali
    Nov 30, 2018 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.