<ol> <li>1</li> <li>2</li> <li>3</li> </ol> <script> $('li').click(function () { $(this); // Например клик по li 2 }); <script> 

For example, the user clicked on li 2 , is it possible to click on li neighbors, in this case 1 and 3 , when clicking on li? If so, how to access them?

There are no identifiers for li. There is only ol inside li . ol on page a few. Allowed to assign only an identifier to the object ol

    1 answer 1

     $('li').click(function() { /*var $lis = $(this).siblings(); // only the other two console.log($lis.length); $lis = $(this).closest("ol").find('li'); // all three console.log($lis.length);*/ // только соседние console.log("previous", $(this).prev().length, $(this).prev().text()); console.log("next", $(this).next().length, $(this).next().text()); }); 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ol> <li>1</li> <li>2</li> <li>3</li> </ol> 

    • closest gets the last item. It turns out if in ol 5 li, then I get not the next (3), but the fifth li - Denis of Heavenly
    • @ Denis the Heavens Where did you get this information? closest finds the closest element in the this chain of parents that satisfies the selector. - Igor
    • @ Denis Heaven What should I make sure? alert($lis.length); shows the number of li elements under the same parent. - Igor
    • When you click on any element returns the last - Denis of Heaven