简体   繁体   中英

To find Next element data-id of list in JQuery

How to get the data-id of next list element from the current active list element on button click?

 <div class="nbrs">
    <ul>
      <li id="item1" data-id="1" class="active">Coffee (first li)</li>
      <li id="item2" data-id="2">Tea (second li)</li>
      <li id="item3" data-id="3">Green Tea (third li)</li>
    </ul>
    </div>

   <button id="btnNext" type="button">Next</button> 

The next element data-id need to be shown till the last (third) li.

you can make use of next() to get next li element and read data-id using .data('id) ... see below code sample

 $(function(){ $('#btnNext').on('click', function(){ var $next = $('ul li.active').next('li'); if($next.length > 0) { var id = $next.data('id'); alert(id); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="nbrs"> <ul> <li id="item1" data-id="1" class="active">Coffee (first li)</li> <li id="item2" data-id="2">Tea (second li)</li> <li id="item3" data-id="3" >Tea (third li)</li> </ul> </div> <button id="btnNext" type="button">Next</button>

You can find next li with .next('li') and find its attribute data-id value with .attr('data-id') . Remove active class from currently active li with $('li.active').removeClass('active'); & add active class in next li with next.addClass('active'); .

Try like below.

 $('#btnNext').click(function() { // find next li from currently active li let next = $('li.active').next('li'); if (next.length > 0) { // remove active class from currently active li $('li.active').removeClass('active'); // add active class in next li next.addClass('active'); // get data id from next li let dataId = next.attr('data-id'); alert(dataId); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script> <div class="nbrs"> <ul> <li id="item1" data-id="1" class="active">Coffee (first li)</li> <li id="item2" data-id="2">Tea (second li)</li> <li id="item3" data-id="3">Green Tea (third li)</li> </ul> </div> <button id="btnNext" type="button">Next</button>

Using vanilla Javascript, you can use the .nextElementSibling() on the li element that has a class of active . Wrap this in a loop that iterates to check the elements length with an iterator. Place a conditional that stops the iteration once we get to the length of the nodeList within an eventlistener on click of the button.

 const active = document.querySelectorAll('li'); const btn = document.getElementById('btnNext'); //--> initialize a counter let i = 0; //--> loop over the list item elements active.forEach((val) => { //--> use the current element being looped over //--> and check to see if it contains class of active if (val.classList.contains('active')) { //--> event listener for the click of the button btnNext.addEventListener('click', () => { //--> here we add a constraint for the loop using.length against the counter //--> if our counter is less than the list items length minus 1 //--> this means we have not reached the end of the list items if (i < active.length - 1) { //--> we use the counter i as a key on the list items element list //--> to get the dataset.id of the nextElementSibling console.log(active[i].nextElementSibling.dataset.id); //--> increase counter by one for the next loop through logic i++; } }) } })
 <div class="nbrs"> <ul> <li id="item1" data-id="1" class="active anotherclass">Coffee (first li)</li> <li id="item2" data-id="2">Tea (second li)</li> <li id="item3" data-id="3">Green Tea (third li)</li> </ul> </div> <button id="btnNext" type="button">Next</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM