-4

I cannot seem to figure out the solution to my problem. Through intense several days of research I've concluded this stack overflow question is similar and may be a solution (GetElementByID - Multiple IDs), but with all my trials I still can't seem to implement the solution to my code. [DEMO]. I've asked the same question many times with variations, but I still keep getting broad answers that never help at all. My question is, How can I create multiple ID'S from the script to perform several open-transitons?

 /*!
  * classie - class helper functions
  * classie.has( elem, 'my-class' ) -> true/false
  * classie.add( elem, 'my-new-class' )
  * classie.remove( elem, 'my-unwanted-class' )
  * classie.toggle( elem, 'my-class' )
  */

 /*jshint browser: true, strict: true, undef: true */
 /*global define: false */

 (function (window) {

     'use strict';

     // class helper functions from bonzo https://github.com/ded/bonzo

     function classReg(className) {
         return new RegExp("(^|\\s+)" + className + "(\\s+|$)");
     }

     // classList support for class management
     // altho to be fair, the api sucks because it won't accept multiple classes at once
     var hasClass, addClass, removeClass;

     if ('classList' in document.documentElement) {
         hasClass = function (elem, c) {
             return elem.classList.contains(c);
         };
         addClass = function (elem, c) {
             elem.classList.add(c);
         };
         removeClass = function (elem, c) {
             elem.classList.remove(c);
         };
     } else {
         hasClass = function (elem, c) {
             return classReg(c).test(elem.className);
         };
         addClass = function (elem, c) {
             if (!hasClass(elem, c)) {
                 elem.className = elem.className + ' ' + c;
             }
         };
         removeClass = function (elem, c) {
             elem.className = elem.className.replace(classReg(c), ' ');
         };
     }

     function toggleClass(elem, c) {
         var fn = hasClass(elem, c) ? removeClass : addClass;
         fn(elem, c);
     }

     var classie = {
         // full names
         hasClass: hasClass,
         addClass: addClass,
         removeClass: removeClass,
         toggleClass: toggleClass,
         // short names
         has: hasClass,
         add: addClass,
         remove: removeClass,
         toggle: toggleClass
     };

     // transport
     if (typeof define === 'function' && define.amd) {
         // AMD
         define(classie);
     } else {
         // browser global
         window.classie = classie;
     }

 })(window);


 /**
  * main.js
  * http://www.codrops.com
  *
  * Licensed under the MIT license.
  * http://www.opensource.org/licenses/mit-license.php
  * 
  * Copyright 2014, Codrops
  * http://www.codrops.com
  */ (function () {

     var bodyEl = document.body,
         content = document.querySelector('.content-wrap'),
         openbtn = document.getElementById('open-button'),
         closebtn = document.getElementById('close-button'),
         isOpen = false;

     function init() {
         initEvents();
     }

     function initEvents() {
         openbtn.addEventListener('click', toggleMenu);
         if (closebtn) {
             closebtn.addEventListener('click', toggleMenu);
         }

         // close the menu element if the target it´s not the menu element or one of its descendants..
         content.addEventListener('click', function (ev) {
             var target = ev.target;
             if (isOpen && target !== openbtn) {
                 toggleMenu();
             }
         });
     }

     function toggleMenu() {
         if (isOpen) {
             classie.remove(bodyEl, 'show-menu');
         } else {
             classie.add(bodyEl, 'show-menu');
         }
         isOpen = !isOpen;
     }

     init();

 })();

Though I figured out specifically.

 var bodyEl = document.body,
         content = document.querySelector('.content-wrap'),
         openbtn = document.getElementById('open-button'),
         closebtn = document.getElementById('close-button'),
         isOpen = false;

They are two buttons shown as three gray dots if you scroll towards the middle of the result screen. The first set of gray dots will open two black boxes. I do not want that. I would only like the first set of gray dots to only open the first box, the second set of gray dots to only open the second box, and so on.

I figured this portion of the javascript was what was needed to be altered in order to achieve this, though like I said before I cannot seem to implement the solution. Can some very bright mind help me figure this out! no advice please!, just a straight forward answer which solves the problem!

CAN SOMEONE PLEASE HELP ME, IVE SEEN USERS ANSWER WITH PARAGRAPHS OF CODE, MY QUESTION IS NOT AT ALL TOO DIFFICULT FOR THE EXPERIENCED AND I KNOW IT HAS BEEN DONE BEFORE!!.

Community
  • 1
  • 1
  • Your question title and body are asking two separate things; which is it? Do you want to know how to use `Document.getElementsByClassName`, or do you want to use JavaScript to add IDs to multiple elements? – TylerH Aug 02 '15 at 05:02
  • Either method would work fine, but more likely how to use Javascript to add IDs to multiple elements. – Sebastien Aug 02 '15 at 05:03
  • 2
    I think this question could be improved if you described the problem that you need a solution for instead of pasting scripts that we need to read to try to understand what problem you are trying to solve. – oldwizard Aug 02 '15 at 05:18
  • I explained the problem towards the end. They are two buttons shown as three gray dots if you scroll towards the middle of the result screen. The first set of gray dots will open two black boxes. I do not want that. I would only like the first set of gray dots to only open the first box, the second set of gray dots to only open the second box, and so on. – Sebastien Aug 02 '15 at 05:19
  • 3
    Yelling to people will not help you to acheive the goal. Also if you want someone to help, you should listen to suggestions gived to you. – bksi Aug 02 '15 at 06:02

2 Answers2

1

No one wants to write your code for you. This is a Q and A site, not a 'fix my code for me' site. Shouting at people in all bold caps won't get you what you need.

Despite having said that... you have two buttons with the same ID, which are trying to 'open' different elements with no IDs. IDs should be distinct, so give your buttons different IDs and attach different click functions to them, or use the same method but pass in the ID of the element to toggle. The click function should modify the class of a particular menu-wrap element, defined by its own distinct ID.

The other issue is that your CSS code modifies the body's class in order to open the menu. You should probably by modifying the class of the particular element you need to show/hide.

Here is a modified version of your code. It somewhat works, but should give you a better idea of what you need to do. You should also look into cleaning up some of your code and possibly look into using jQuery, as it is nicer on new developers.

haferje
  • 958
  • 2
  • 14
  • 21
0

You are using .show-menu .menu-wrap{...} to open the divs and when you click on the gray dots you set the class .show-menu to the body so both divs are been affected by the style .show-menu .menu-wrap because both are inside the body.

The way to fix it is to put a wrapper around them and toggle .show-menu to the parent of the gray dots element that you click like this:

$(".menu-button").click(function () {
    $(this).parents(".wrapper").toggleClass("show-menu");
});
body, .container, .content-wrap {
    width: 100%;
    height: 100%;
}
.container {
    background: #fff;
}
.menu-wrap a {
    color: #b8b7ad;
}
.menu-wrap a:hover, .menu-wrap a:focus {
    color: #c94e50;
}
.content-wrap {
    overflow-y: scroll;
    -webkit-overflow-scrolling: touch;
}
.content {
    position: absolute;
    background: #fff;
    overflow-y: visible;
}
.content::before {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 10;
    width: 100%;
    height: 100%;
    background: none;
    content:'';
    opacity: 0;
    -webkit-transform: translate3d(100%, 0, 0);
    transform: translate3d(100%, 0, 0);
    -webkit-transition: opacity 0.4s, -webkit-transform 0s 0.4s;
    transition: opacity 0.4s, transform 0s 0.4s;
    -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
}
/* Menu Button 1 */
 .menu-button {
    position: absolute;
    z-index: 1000;
    margin: 1em;
    padding: 0;
    width: 10px;
    height: 50px;
    border: none;
    text-indent: 2.5em;
    font-size: 1.5em;
    color: transparent;
    background: transparent;
    opacity: 1;
    top: 510px;
    -ms-transform: rotate(7deg);
    /* IE 9 */
    -webkit-transform: rotate(7deg);
    /* Chrome, Safari, Opera */
    transform: rotate(90deg);
    cursor: pointer;
}
.menu-button::before {
    position: absolute;
    top: 0.5em;
    right: 0.2em;
    bottom: 0.4em;
    left: -1px;
    background: linear-gradient(#929292 20%, transparent 20%, transparent 40%, #929292 40%, #929292 58%, transparent 0%, transparent 80%, #929292 80%);
    content:'';
}
.menu-button:hover {
    opacity: 1;
}
/* Close Button */
 .close-button {
    width: 50px;
    height: 50px;
    position: absolute;
    right: 1em;
    top: 1em;
    overflow: hidden;
    text-indent: 1em;
    font-size: 0.75em;
    border: none;
    background: transparent;
    color: transparent;
    opacity: 0;
}
.close-button::before, .close-button::after {
    content:'';
    position: absolute;
    width: 3px;
    height: 100%;
    top: 0;
    left: 50%;
    background: #bdc3c7;
}
.close-button::before {
    -webkit-transform: rotate(45deg);
    transform: rotate(45deg);
}
.close-button::after {
    -webkit-transform: rotate(-45deg);
    transform: rotate(-45deg);
}
/* Comments */
 .menu-wrap {
    position: absolute;
    z-index: 1000;
    width: 429.0500011444092px;
    height: 600.875px;
    right: 0;
    background: #0C0C0C;
    top: 6px;
    padding: 0;
    font-size: 1.15em;
    -webkit-transform: translate3d(500px, 0, 0);
    transform: translate3d(500px, 0, 0);
    -webkit-transition: -webkit-transform 0.4s;
    transition: transform 0.4s;
    -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
}
.menu, .icon-list {
    height: 100%;
}
.icon-list {
    -webkit-transform: translate3d(0, 100%, 0);
    transform: translate3d(0, 100%, 0);
}
.icon-list a {
    display: block;
    padding: 0.8em;
    -webkit-transform: translate3d(0, 500px, 0);
    transform: translate3d(0, 500px, 0);
}
.icon-list, .icon-list a {
    -webkit-transition: -webkit-transform 0s 0.4s;
    transition: transform 0s 0.4s;
    -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
}
.icon-list a:nth-child(2) {
    -webkit-transform: translate3d(0, 1000px, 0);
    transform: translate3d(0, 1000px, 0);
}
.icon-list a:nth-child(3) {
    -webkit-transform: translate3d(0, 1500px, 0);
    transform: translate3d(0, 1500px, 0);
}
.icon-list a:nth-child(4) {
    -webkit-transform: translate3d(0, 2000px, 0);
    transform: translate3d(0, 2000px, 0);
}
.icon-list a:nth-child(5) {
    -webkit-transform: translate3d(0, 2500px, 0);
    transform: translate3d(0, 2500px, 0);
}
.icon-list a:nth-child(6) {
    -webkit-transform: translate3d(0, 3000px, 0);
    transform: translate3d(0, 3000px, 0);
}
.icon-list a span {
    margin-left: 10px;
    font-weight: 700;
}
/* Shown menu */
 .show-menu .menu-wrap {
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
    -webkit-transition: -webkit-transform 0.8s;
    transition: transform 0.8s;
    -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
}
.show-menu .icon-list, .show-menu .icon-list a {
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
    -webkit-transition: -webkit-transform 0.8s;
    transition: transform 0.8s;
    -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
}
.show-menu .icon-list a {
    -webkit-transition-duration: 0.9s;
    transition-duration: 0.9s;
}
.show-menu .content::before {
    opacity: 1;
    -webkit-transition: opacity 0.8s;
    transition: opacity 0.8s;
    -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
    <div class="wrapper">
        <button class="menu-button">Open Menu</button>
        <div class="menu-wrap"></div>
    </div>
    <div class="wrapper">
        <button class="menu-button" style="
    top: 560px;
">Open Menu</button>
        <div class="menu-wrap" style="
    top: 700px;
"></div>
    </div>
</body>

Or if you don't want to change your markup just toggle the class .show-menu to the .menu-wrap itself and change the selector to this .show-menu.menu-wrap{...} like this:

$(".menu-button").click(function () {
    $(this).next(".menu-wrap").toggleClass("show-menu");
});
body, .container, .content-wrap {
    width: 100%;
    height: 100%;
}
.container {
    background: #fff;
}
.menu-wrap a {
    color: #b8b7ad;
}
.menu-wrap a:hover, .menu-wrap a:focus {
    color: #c94e50;
}
.content-wrap {
    overflow-y: scroll;
    -webkit-overflow-scrolling: touch;
}
.content {
    position: absolute;
    background: #fff;
    overflow-y: visible;
}
.content::before {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 10;
    width: 100%;
    height: 100%;
    background: none;
    content:'';
    opacity: 0;
    -webkit-transform: translate3d(100%, 0, 0);
    transform: translate3d(100%, 0, 0);
    -webkit-transition: opacity 0.4s, -webkit-transform 0s 0.4s;
    transition: opacity 0.4s, transform 0s 0.4s;
    -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
}
/* Menu Button 1 */
 .menu-button {
    position: absolute;
    z-index: 1000;
    margin: 1em;
    padding: 0;
    width: 10px;
    height: 50px;
    border: none;
    text-indent: 2.5em;
    font-size: 1.5em;
    color: transparent;
    background: transparent;
    opacity: 1;
    top: 510px;
    -ms-transform: rotate(7deg);
    /* IE 9 */
    -webkit-transform: rotate(7deg);
    /* Chrome, Safari, Opera */
    transform: rotate(90deg);   
    cursor: pointer;
}
.menu-button::before {
    position: absolute;
    top: 0.5em;
    right: 0.2em;
    bottom: 0.4em;
    left: -1px;
    background: linear-gradient(#929292 20%, transparent 20%, transparent 40%, #929292 40%, #929292 58%, transparent 0%, transparent 80%, #929292 80%);
    content:'';
}
.menu-button:hover {
    opacity: 1;
}
/* Close Button */
 .close-button {
    width: 50px;
    height: 50px;
    position: absolute;
    right: 1em;
    top: 1em;
    overflow: hidden;
    text-indent: 1em;
    font-size: 0.75em;
    border: none;
    background: transparent;
    color: transparent;
    opacity: 0;
}
.close-button::before, .close-button::after {
    content:'';
    position: absolute;
    width: 3px;
    height: 100%;
    top: 0;
    left: 50%;
    background: #bdc3c7;
}
.close-button::before {
    -webkit-transform: rotate(45deg);
    transform: rotate(45deg);
}
.close-button::after {
    -webkit-transform: rotate(-45deg);
    transform: rotate(-45deg);
}
/* Comments */
 .menu-wrap {
    position: absolute;
    z-index: 1000;
    width: 429.0500011444092px;
    height: 600.875px;
    right: 0;
    background: #0C0C0C;
    top: 6px;
    padding: 0;
    font-size: 1.15em;
    -webkit-transform: translate3d(500px, 0, 0);
    transform: translate3d(500px, 0, 0);
    -webkit-transition: -webkit-transform 0.4s;
    transition: transform 0.4s;
    -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
}
.menu, .icon-list {
    height: 100%;
}
.icon-list {
    -webkit-transform: translate3d(0, 100%, 0);
    transform: translate3d(0, 100%, 0);
}
.icon-list a {
    display: block;
    padding: 0.8em;
    -webkit-transform: translate3d(0, 500px, 0);
    transform: translate3d(0, 500px, 0);
}
.icon-list, .icon-list a {
    -webkit-transition: -webkit-transform 0s 0.4s;
    transition: transform 0s 0.4s;
    -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
}
.icon-list a:nth-child(2) {
    -webkit-transform: translate3d(0, 1000px, 0);
    transform: translate3d(0, 1000px, 0);
}
.icon-list a:nth-child(3) {
    -webkit-transform: translate3d(0, 1500px, 0);
    transform: translate3d(0, 1500px, 0);
}
.icon-list a:nth-child(4) {
    -webkit-transform: translate3d(0, 2000px, 0);
    transform: translate3d(0, 2000px, 0);
}
.icon-list a:nth-child(5) {
    -webkit-transform: translate3d(0, 2500px, 0);
    transform: translate3d(0, 2500px, 0);
}
.icon-list a:nth-child(6) {
    -webkit-transform: translate3d(0, 3000px, 0);
    transform: translate3d(0, 3000px, 0);
}
.icon-list a span {
    margin-left: 10px;
    font-weight: 700;
}
/* Shown menu */
 .show-menu.menu-wrap {
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
    -webkit-transition: -webkit-transform 0.8s;
    transition: transform 0.8s;
    -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
}
.show-menu .icon-list, .show-menu .icon-list a {
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
    -webkit-transition: -webkit-transform 0.8s;
    transition: transform 0.8s;
    -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
}
.show-menu .icon-list a {
    -webkit-transition-duration: 0.9s;
    transition-duration: 0.9s;
}
.show-menu .content::before {
    opacity: 1;
    -webkit-transition: opacity 0.8s;
    transition: opacity 0.8s;
    -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
    <button class="menu-button">Open Menu</button>
    <div class="menu-wrap"></div>
    <button class="menu-button" style="
    top: 560px;
">Open Menu</button>
    <div class="menu-wrap" style="
    top: 700px;
"></div>
</body>

The above examples just give you a general idea, homework do it using pure javascript but I really recommend you use jquery.

Yandy_Viera
  • 4,320
  • 4
  • 21
  • 42