Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Give element class when specific element has active class

Is it possible with Jquery to add another class to the .body element when the third li element has an .active class? It cant just add an class based on the .active class, it has to be the .active on the third li element specifically.

<div id="extendedinfobox">
   <ul class="prodtabmenu">
    <li></li>
    <li></li>
    <li class="active"></li>
    <li></li>
    <li></li>
  </ul>
 </div>
<div class="body">

like image 857
Jerry Avatar asked Apr 10 '26 20:04

Jerry


2 Answers

https://jsfiddle.net/sn2nc9bd/1/

if($('ul.prodtabmenu').find('li:nth-child(3)').hasClass('active'))
{
   $('.body').addClass('newClass');   
}
like image 102
jQuery00 Avatar answered Apr 12 '26 08:04

jQuery00


if ($('ul.prodtabmenu').find('li:nth-child(3)').hasClass('active')) {
  //$('body').addClass('disabled');
  $('.body').addClass('disabled');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="extendedinfobox">
  <ul class="prodtabmenu">
    <li>2</li>
    <li>21</li>
    <li class="active">3</li>
    <li>4</li>
    <li>1</li>
  </ul>
</div>
<div class="body">

Try like this

Use .nth-child()

Description: Selects all elements that are the nth-child of their parent.

use .nth-child(3) to select the 3rd li

like image 38
guradio Avatar answered Apr 12 '26 09:04

guradio



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!