Skip to content
  • Recent
  • Tags
  • Popular
  • Users
  • Search
Skins
  • Light
  • Brite
  • 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.6k Views 1 Watching
  • 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
    C
    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
      mariusjopenM
      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
        C
        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
          C
          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
            mariusjopenM
            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

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            Online Users
            _
            _Luccer
            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