SBB
SBB

Reputation: 8970

jQuery getting attribute of a tab

I am using bootstrap tabs in my application and on change of a dropdown, i want to find out what tab is currently active and from that, get an attribute from the active tab.

So far I have this: $("ul#depts li.active").text(); which gets me the text of the active tab.

However, when I try something like ("ul#depts li.active").attr('departmentid') nothing is returned.

<ul id="depts" role="tablist" class="nav nav-tabs">
                  <li class=""><a data-toggle="tab" role="tab" href="#Disputes" departmentid="2" name="switchDepartment" class="switch">Disputes</a></li>
                  <li class="active"><a data-toggle="tab" role="tab" href="#ICA" departmentid="5" name="switchDepartment" class="switch">ICA</a></li>
               </ul>

What am I missing?

Upvotes: 0

Views: 83

Answers (2)

Vedant Terkar
Vedant Terkar

Reputation: 4682

Two basic corrections:

  1. Your're missing $ sign in: ("ul#depts li.active").attr('departmentid')

  2. Deptid is attribute of <a> element inside that li so it'll be

$("ul#depts li.active a").attr('departmentid')

DEMO

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

as per your posted HTML, you have set attribute in anchor tag not in li, so change to:

("ul#depts li.active > a").attr('departmentid');

Upvotes: 3

Related Questions