Get the previous element with .prev()

.prev()


Description: Get the previous element that's before the first matched element.


Example:

<!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>prev demo</title>
      <script src="http://code.bitsycode.com/bitsycode-latest.min.js"></script>
      <style type="text/css">
         .active {
            background-color:yellow;
         }
      </style>
    </head>
    <body>
      
      <ul class="myList">
         <li>One</li>
         <li>Two</li>
         <li class="active">Three</li>
      </ul>
      
      <button onclick="prev()">Prev</button>
      
      <script>
        
        function prev() {
            
            if ((bitsyCode(".active").prev()).nodes.length > 0) {
                bitsyCode(".active").removeClass("active").prev().addClass("active");
            } else {
                bitsyCode(".myList").first().removeClass("active");
                bitsyCode(".myList").last().addClass("active");
            }            
            
        }
         
      </script>
     
    </body>
    </html>

Preview:

  • One
  • Two
  • Three
  • Usage

    • How to use:

      Get the previous element that's before the first matched element.

    • USAGE:

      bitsyCode("element").prev()
  • Options

    • What options are there:

      There are no options for this method.

  • Returns

    • Returns: the previous element before the first matched element.
Share