Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find first next element and then stop the search

How can use I jQuery to select the first element coming after (not immediate) a specific element? I have this list.

<ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li class="tr current">Item 3</li>
    <li>Item 4</li>
    <li class="tr">Item 5</li>                                          
    <li>Item 6</li>            
    <li class="tr">Item 7</li>                        
    <li>Item 8</li>   
</ul>

How can I select/manipulate the first coming tr (Item 5) using tr current (Item 3) using jQuery? I have tried this.

// This apply background color to the next immediate (Item 4) as its definition.
$('.tr.current').next().css('background-color', '#000');

// This apply to background color all next Items
$('.tr.current').nextAll().css('background-color', '#000');     

// This apply to background color all next Items with class tr (Item 5, Item 7)
$('.tr.current').next('.tr').css('background-color', '#000');       
like image 341
Awais Umar Avatar asked Nov 30 '25 03:11

Awais Umar


1 Answers

use .nextAll() with :first selector:

$('.tr.current').nextAll('.tr:first').css('background-color', '#000');     
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<ul>
            <li>Item 1</li>
            <li>Item 2</li>
            <li class="tr current">Item 3</li>
            <li>Item 4</li>
            <li class="tr">Item 5</li>                                          
            <li>Item 6</li>            
            <li class="tr">Item 7</li>                        
            <li>Item 8</li>                                    
</ul>
like image 78
Milind Anantwar Avatar answered Dec 01 '25 16:12

Milind Anantwar



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!