Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 418
Shivkumar Avatar asked Apr 03 '13 06:04

Shivkumar


People also ask

How to get the id of a div in jQuery?

Answer: Use the jQuery attr() Method You can simply use the jQuery attr() method to get or set the ID attribute value of an element. The following example will display the ID of the DIV element in an alert box on button click.

How to find id with jQuery?

The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.

How do I select a class in jQuery?

In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.

Why this is used in jQuery?

HTML. The this Keyword is a reference to DOM elements of invocation. We can call all DOM methods on it. $() is a jQuery constructor and in $(this), we are just passing this as a parameter so that we can use the jQuery function and methods.


1 Answers

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');
like image 190
Adil Avatar answered Oct 23 '22 02:10

Adil