Skip to content
  • Recent
  • Tags
  • Popular
  • Users
  • Search
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse

Lay Theme Forum

  1. Home
  2. General Discussion
  3. Custom Javascript question

Custom Javascript question

Scheduled Pinned Locked Moved General Discussion
5 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    clineker
    wrote on last edited by
    #1

    Hi there,

    I have read the other threads about adding an accordion with custom script but they still don't seem to help my problem.
    I am trying to add this Accordion to my site

    https://www.w3schools.com/howto/howto_js_accordion.asp

    I currently have:

    In the project and under +more - +HTML I have

     <button class="accordion">Section 1</button>
     <div class="panel">
     <p>COPY INSIDE TAB</p>
     </div>
    

    Under Custom CSS for Desktop Version

    .accordion {
    background-color: #eee;
    color: #444;
    cursor: pointer;
    padding: 18px;
    width: 100%;
    border: none;
    text-align: left;
    outline: none;
    font-size: 15px;
    transition: 0.4s;
    }
    
    .active, .accordion:hover {
    background-color: #ccc; 
    }
    
    .panel {
    padding: 0 18px;
    display: none;
    background-color: white;
    

    And finally in my Custom <head> content

        <script>
        window.laytheme.on("newpageshown", function(){
        }   
        var acc = document.getElementsByClassName("accordion");
        var i;
    
       for (i = 0; i < acc.length; i++) {
       acc[i].addEventListener("click", function() {
       this.classList.toggle("active");
    
        /* Toggle between hiding and showing the active panel */
        var panel = this.nextElementSibling;
        if (panel.style.display === "block") {
            panel.style.display = "none";
        } else {
            panel.style.display = "block";
        }
        });
        } 
        </script>
    

    Would you be able to let me know what I am doing wrong? The panel is appearing fine - its the Javascript thats not working.

    Regards

    Chris

    1 Reply Last reply
    0
    • mariusjopenM Offline
      mariusjopenM Offline
      mariusjopen
      Global Moderator
      wrote on last edited by
      #2

      Hi @clineker

      I would recommend to use a simpler version of an accordeon. With jQuery:
      https://jsfiddle.net/harrisonstatham/WvtEw/

      This code with the loop looks a bit oldschool.

      Have a look here how you bind 'click events:
      http://laytheme.com/documentation.html#custom-javascript

      Good luck!

      Marius

      www.mariusjopen.world

      1 Reply Last reply
      0
      • C Offline
        C Offline
        clineker
        wrote on last edited by clineker
        #3

        @clineker said in Custom Javascript question:

        <div class="panel">

        Hi @mariusjopen Marius,

        Thank you for your quick reply!

        I am only just getting into using JQuery and so am a little confused as to where I am inserting JQuerys "on" function.

        I currently have in my Javascript as:

             <script>
        
            window.laytheme.on("newpageshown", function(){
              // display the first div by default.
              $jQuery("#accordion div").first().css('display', 'block');
        
        
             // Get all the links.
            var link = jQuery("#accordion a");
        
             // On clicking of the links do something.
              link.on('click', function(e) {
        
              e.preventDefault();
        
              var a = jQuery(this).attr("href");
        
               jQuery(a).slideDown('fast');
        
              //jQuery(a).slideToggle('fast');
               jQuery("#accordion div").not(a).slideUp('fast');
        
               });
              </script>
        

        Taken from the above link and replacing the dollar sign with JQuery.

        If you could help me, I would be massively appreciated.

        The page im trying to get it to work on is

        www.chrislineker.space/test

        Hope to hear form you soon

        Chris

        1 Reply Last reply
        0
        • C Offline
          C Offline
          clineker
          wrote on last edited by
          #4

          UPDATE:

          Firgured it out guys!

          My Javascript ended up looking like the below

           <script>
          
           // display the first div by default.
           jQuery("#accordion div").first().css('display', 'none');
          
           // Get all the links.
           var link = jQuery("#accordion a");
          
           // On clicking of the links do something.
                jQuery(document).on("click",".accordion a", function(e) {
          
               e.preventDefault();
          
               var id = jQuery(this).attr('href');
             jQuery(id).slideToggle();
           });
           </script>
          

          Thanks for all the help !

          Cheers

          Chris

          1 Reply Last reply
          0
          • mariusjopenM Offline
            mariusjopenM Offline
            mariusjopen
            Global Moderator
            wrote on last edited by
            #5

            Hi Chris!

            Glad you figured that out!

            Happy to hear!

            Marius

            www.mariusjopen.world

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            I also code custom websites or custom Lay features.
            šŸ’æ Email me here: šŸ’æ
            info@laytheme.com

            Before you post:
            1. When using a WordPress Cache plugin, disable it or clear your cache.
            2. Update Lay Theme and all Lay Theme Addons
            3. Disable all Plugins
            4. Go to Lay Options → Custom CSS & HTML, click "Turn Off All Custom Code", click "Save Changes"

            This often solves issues you might run into

            When you post:
            1. Post a link to where the problem is
            2. Does the problem happen on Chrome, Firefox, Safari or iPhone or Android?
            3. If the problem is difficult to explain, post screenshots / link to a video to explain it
            Online Users
            J
            JulianeEirich
            dafvD
            dafv
            J
            jacksong
            daniD
            dani
            A
            alasdair17
            Forgot your key, lost your files, need a previous Lay Theme or Addon version? Go to www.laykeymanager.com
            laytheme.com
            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Recent
            • Tags
            • Popular
            • Users
            • Search