• Geek Answers Handbook

    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
    javascript jquery html
    Shivkumar Apr 3 '13 at 6:27
    source share
    5 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 Apr 3 '13 at 6:28
    source share

    It 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 Apr 3 '13 at 6:31
    source share

    You can get any HTML attribute from an element using jQuery .attr() :

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

    Similarly, you can also get the property value of a JS object using .prop() :

     $('.tab-pane.active').prop('id'); 
    +1
    itsstephyoutrick Apr 3 '13 at 6:29
    source share

    You can try this

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

    Hope this helps

    0
    Roger Apr 3 '13 at 6:31
    source share

    Try the following:

      $('#addspan').click(function () { alert($('.tab-pane .active').attr('id')); }); 
    0
    ravisolanki07 Apr 3 '13 at 13:55
    source share

    More articles:

    • UITableViewCell textColor will not be changed with userInteractionEnabled = NO - iphone
    • spring hibernate manual commit - spring
    • Why is one var good in javascript? - javascript
    • resolve address from overloaded function std :: real - c ++
    • Python openCV detects parallel lines - python
    • How to reference dll in Visual Studio 2010? - c ++
    • Static UITableViewCell changes while viewing WillAppear not showing on display - ios
    • Problems with build.phonegap write to file - javascript
    • change the color of a UITableViewCell after reusing user interaction - uitableview
    • How to get yahoo woeid by location? - geolocation

    All Articles

    Geek Answers | 2019