Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By Ronald Roe
  • 1 Post By Ronald Roe

Thread: can someone tell me?

  1. #1
    Senior Member
    Join Date
    Apr 2016
    Posts
    717
    Member #
    53891
    Liked
    11 times

    can someone tell me?

    var acc = document.getElementsByClassName("content-title");
    var i;

    for (i = 0; i < acc.length; i++) {
    acc[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var panel = this.nextElementSibling;
    if (panel.style.display === "block") {
    panel.style.display = "none";
    } else {
    panel.style.display = "block";
    }
    });
    }
    can someone tell me what the code on line 6 does? the script doesn't work without it

  2.  

  3. #2
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    I assume you're talking about the nextElementSibling bit? That stores the current element's next sibling element in the variable. In other words if you have:
    HTML Code:
    <ul>
      <li>Some content 1</li>
      <li>Some content 2</li>
      <li>Some content 3</li>
    </ul>
    If you select the second li, nextElementSibling refers to the third one. It's a helper method that makes DOM traversal easier. Without it, you'd have to use .parentElement, to figure out which element you're looking at, and then figure out which element is the next one, and so on.

    In most cases, you'd use this.parentElement.children[i + 1], but that won't always get you what you want.
    Last edited by Ronald Roe; Jan 31st, 2018 at 08:45 AM.
    satindertech likes this.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  4. #3
    Senior Member
    Join Date
    Apr 2016
    Posts
    717
    Member #
    53891
    Liked
    11 times
    i meant this line but thanks

    this.classList.toggle("active");

  5. #4
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    Oh. That's like jQuery's .toggle(). If the element has the class "active", it removes it. If not, it adds it. It literally "toggles" the class.
    satindertech likes this.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 02:46 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com