Shivkumar
Shivkumar

Reputation: 1903

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

Upvotes: 7

Views: 29030

Answers (6)

Ambrose Jesuraj
Ambrose Jesuraj

Reputation: 143

Try the following code

$("#addspan").click(function(){console.log($(this)[0].id);});

Upvotes: 0

itsstephyoutrick
itsstephyoutrick

Reputation: 216

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');

Upvotes: 0

ravisolanki07
ravisolanki07

Reputation: 647

Try this :

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

Upvotes: 0

Chamika Sandamal
Chamika Sandamal

Reputation: 24302

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.

Upvotes: 2

Roger
Roger

Reputation: 1693

You can try this

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

Hope this helps

Upvotes: 0

Adil
Adil

Reputation: 148110

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');

Upvotes: 14

Related Questions