Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the element which has class active?

I've a list. I want to get the element of the id which has class active.

<ul class="nav nav-tabs" id="nav">
   <li class="active" id="day" onclick="changeDur('day')"><a data-toggle="tab">Day</a></li>
   <li id="week" onclick="changeDur('week')"><a data-toggle="tab">Week</a></li>
   <li id="month" onclick="changeDur('month')"><a data-toggle="tab">Month</a></li>
   <li id="year" onclick="changeDur('year')"><a data-toggle="tab">Year</a></li>
</ul>

Here's what I'm trying?

var period_val = $("#nav.active").attr('id');
alert(period_val)

It gives me undefined.

Where I'm going wrong?

like image 996
python-coder Avatar asked May 11 '26 03:05

python-coder


1 Answers

var period_val = $("#nav .active").attr('id');

You need a space to select descendent elements.

like image 124
ahren Avatar answered May 12 '26 18:05

ahren



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!