enltr xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/terms/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:og="http://ogp.me/ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:sioc="http://rdfs.org/sioc/ns#" xmlns:sioct="http://rdfs.org/sioc/types#" xmlns:skos="http://www.w3.org/2004/02/skos/core#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#"http://www.w3.org/1999/xhtml/vocab/////////////////Get the previous element with .prev() | BitsyCodehttps://www.bitsycode.com/methods/dom/prevGet the previous element with .prev() | BitsyCode Get the previous element with .prev() | BitsyCode html not-front not-logged-in no-sidebars page-node page-node- page-node-60 node-type-page

Get the previous element with .prev()

container

.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:

Share

Developed and built with passion and commitment.
Maintained by the bitsycode team.