querySelectorAll with forEach function not working for toggle button on carousel slides Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!HTML href with css ie Problemanimating addClass/removeClass with jQueryHow do I change the class of a <div> that has an ID?Stop content hiding under nav bar when user scrollsWhy doesn't translateX work as expected for fixed elements on IE9, IE10, and IE11?Cannot display HTML string'Skips' when using Bootstrap's AffixHow can I insert html code inside a javascript value?Bootstrap 3 - Toggle - 2 buttons, 2 elements - How to show/hide?inner divs do not get vertically centered or left aligned

Was the pager message from Nick Fury to Captain Marvel unnecessary?

Should man-made satellites feature an intelligent inverted "cow catcher"?

Noise in Eigenvalues plot

Pointing to problems without suggesting solutions

Does a random sequence of vectors span a Hilbert space?

Where and when has Thucydides been studied?

Twin's vs. Twins'

3D Masyu - A Die

How do I say "this must not happen"?

How does the body cool itself in a stillsuit?

One-one communication

First paper to introduce the "principal-agent problem"

Why complex landing gears are used instead of simple, reliable and light weight muscle wire or shape memory alloys?

How to resize main filesystem

Getting representations of the Lie group out of representations of its Lie algebra

Are there any irrational/transcendental numbers for which the distribution of decimal digits is not uniform?

The Nth Gryphon Number

Did John Wesley plagiarize Matthew Henry...?

Why is there so little support for joining EFTA in the British parliament?

Can two people see the same photon?

Weaponising the Grasp-at-a-Distance spell

Where did Ptolemy compare the Earth to the distance of fixed stars?

My mentor says to set image to Fine instead of RAW — how is this different from JPG?

Any stored/leased 737s that could substitute for grounded MAXs?



querySelectorAll with forEach function not working for toggle button on carousel slides



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!HTML href with css ie Problemanimating addClass/removeClass with jQueryHow do I change the class of a <div> that has an ID?Stop content hiding under nav bar when user scrollsWhy doesn't translateX work as expected for fixed elements on IE9, IE10, and IE11?Cannot display HTML string'Skips' when using Bootstrap's AffixHow can I insert html code inside a javascript value?Bootstrap 3 - Toggle - 2 buttons, 2 elements - How to show/hide?inner divs do not get vertically centered or left aligned



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I've created a template part in PHP that copies a button to each slide in a carousel using fullpage.js. The template part has a hidden div that should open up navigation for each slide. Trying the code below, I can only get this button to work on the first slide. I'm thinking an iterated class name might help, but not sure why querySelectorAll wouldn't do it. Any advice appreciated...



http://www.pulsecreative-clients.com/staging/hogshead/#golf






 const clickOnMe = document.querySelectorAll(".course-button");

let clickOnMe = document.querySelectorAll(".course-button");
Array.from(clickOnMe).forEach(el =>
el.addEventListener("click", e =>
let showBox = e.currentTarget.nextElementSibling;
showBox.classList.toggle("open-nav");
);
);

.subnav-content 
position: fixed;
bottom: 15%;
z-index: 1;
box-sizing: border-box;
padding: 10px;
background-color: #000;
display: none;


.golfcoursebutton
box-sizing: content-box;
min-width: 30px;
height: 30px;
padding: 4px;
margin: 4px;
background-color: #fff;
color: #000;
text-align: center;


.open-nav
display: block;

<div id="jump-button" class="jumpbuttons-container">
<div class="subnav">
<button class="course-button">
JUMP TO <i class="fa fa-angle-up"></i>
</button>
<div class="subnav-content">
<div style="display: flex;">
<div class="golfcoursebutton"><a href="#golf/1">1</a></div>
<div class="golfcoursebutton"><a href="#golf/2">2</a></div>
<div class="golfcoursebutton"><a href="#golf/3">3</a></div>
<div class="golfcoursebutton"><a href="#golf/4">4</a></div>
<div class="golfcoursebutton"><a href="#golf/5">5</a></div>
<div class="golfcoursebutton"><a href="#golf/6">6</a></div>
</div>

<div style="display: flex;">
<div class="golfcoursebutton"><a href="#golf/7">7</a></div>
<div class="golfcoursebutton"><a href="#golf/8">8</a></div>
<div class="golfcoursebutton"><a href="#golf/9">9</a></div>
<div class="golfcoursebutton"><a href="#golf/10">10</a></div>
<div class="golfcoursebutton"><a href="#golf/11">11</a></div>
<div class="golfcoursebutton"><a href="#golf/12">12</a></div>
</div>

<div style="display: flex;">
<div class="golfcoursebutton"><a href="#golf/13">13</a></div>
<div class="golfcoursebutton"><a href="#golf/14">14</a></div>
<div class="golfcoursebutton"><a href="#golf/15">15</a></div>
<div class="golfcoursebutton"><a href="#golf/16">16</a></div>
<div class="golfcoursebutton"><a href="#golf/17">17</a></div>
<div class="golfcoursebutton"><a href="#golf/18">18</a></div>
</div>
</div>
</div>
</div>





EDIT
UPDATED WITH SOLUTION










share|improve this question
























  • convert it to an array const clickOnMe = Array.from(document.querySelectorAll(".course-button"))

    – Get Off My Lawn
    Mar 8 at 22:01

















1















I've created a template part in PHP that copies a button to each slide in a carousel using fullpage.js. The template part has a hidden div that should open up navigation for each slide. Trying the code below, I can only get this button to work on the first slide. I'm thinking an iterated class name might help, but not sure why querySelectorAll wouldn't do it. Any advice appreciated...



http://www.pulsecreative-clients.com/staging/hogshead/#golf






 const clickOnMe = document.querySelectorAll(".course-button");

let clickOnMe = document.querySelectorAll(".course-button");
Array.from(clickOnMe).forEach(el =>
el.addEventListener("click", e =>
let showBox = e.currentTarget.nextElementSibling;
showBox.classList.toggle("open-nav");
);
);

.subnav-content 
position: fixed;
bottom: 15%;
z-index: 1;
box-sizing: border-box;
padding: 10px;
background-color: #000;
display: none;


.golfcoursebutton
box-sizing: content-box;
min-width: 30px;
height: 30px;
padding: 4px;
margin: 4px;
background-color: #fff;
color: #000;
text-align: center;


.open-nav
display: block;

<div id="jump-button" class="jumpbuttons-container">
<div class="subnav">
<button class="course-button">
JUMP TO <i class="fa fa-angle-up"></i>
</button>
<div class="subnav-content">
<div style="display: flex;">
<div class="golfcoursebutton"><a href="#golf/1">1</a></div>
<div class="golfcoursebutton"><a href="#golf/2">2</a></div>
<div class="golfcoursebutton"><a href="#golf/3">3</a></div>
<div class="golfcoursebutton"><a href="#golf/4">4</a></div>
<div class="golfcoursebutton"><a href="#golf/5">5</a></div>
<div class="golfcoursebutton"><a href="#golf/6">6</a></div>
</div>

<div style="display: flex;">
<div class="golfcoursebutton"><a href="#golf/7">7</a></div>
<div class="golfcoursebutton"><a href="#golf/8">8</a></div>
<div class="golfcoursebutton"><a href="#golf/9">9</a></div>
<div class="golfcoursebutton"><a href="#golf/10">10</a></div>
<div class="golfcoursebutton"><a href="#golf/11">11</a></div>
<div class="golfcoursebutton"><a href="#golf/12">12</a></div>
</div>

<div style="display: flex;">
<div class="golfcoursebutton"><a href="#golf/13">13</a></div>
<div class="golfcoursebutton"><a href="#golf/14">14</a></div>
<div class="golfcoursebutton"><a href="#golf/15">15</a></div>
<div class="golfcoursebutton"><a href="#golf/16">16</a></div>
<div class="golfcoursebutton"><a href="#golf/17">17</a></div>
<div class="golfcoursebutton"><a href="#golf/18">18</a></div>
</div>
</div>
</div>
</div>





EDIT
UPDATED WITH SOLUTION










share|improve this question
























  • convert it to an array const clickOnMe = Array.from(document.querySelectorAll(".course-button"))

    – Get Off My Lawn
    Mar 8 at 22:01













1












1








1








I've created a template part in PHP that copies a button to each slide in a carousel using fullpage.js. The template part has a hidden div that should open up navigation for each slide. Trying the code below, I can only get this button to work on the first slide. I'm thinking an iterated class name might help, but not sure why querySelectorAll wouldn't do it. Any advice appreciated...



http://www.pulsecreative-clients.com/staging/hogshead/#golf






 const clickOnMe = document.querySelectorAll(".course-button");

let clickOnMe = document.querySelectorAll(".course-button");
Array.from(clickOnMe).forEach(el =>
el.addEventListener("click", e =>
let showBox = e.currentTarget.nextElementSibling;
showBox.classList.toggle("open-nav");
);
);

.subnav-content 
position: fixed;
bottom: 15%;
z-index: 1;
box-sizing: border-box;
padding: 10px;
background-color: #000;
display: none;


.golfcoursebutton
box-sizing: content-box;
min-width: 30px;
height: 30px;
padding: 4px;
margin: 4px;
background-color: #fff;
color: #000;
text-align: center;


.open-nav
display: block;

<div id="jump-button" class="jumpbuttons-container">
<div class="subnav">
<button class="course-button">
JUMP TO <i class="fa fa-angle-up"></i>
</button>
<div class="subnav-content">
<div style="display: flex;">
<div class="golfcoursebutton"><a href="#golf/1">1</a></div>
<div class="golfcoursebutton"><a href="#golf/2">2</a></div>
<div class="golfcoursebutton"><a href="#golf/3">3</a></div>
<div class="golfcoursebutton"><a href="#golf/4">4</a></div>
<div class="golfcoursebutton"><a href="#golf/5">5</a></div>
<div class="golfcoursebutton"><a href="#golf/6">6</a></div>
</div>

<div style="display: flex;">
<div class="golfcoursebutton"><a href="#golf/7">7</a></div>
<div class="golfcoursebutton"><a href="#golf/8">8</a></div>
<div class="golfcoursebutton"><a href="#golf/9">9</a></div>
<div class="golfcoursebutton"><a href="#golf/10">10</a></div>
<div class="golfcoursebutton"><a href="#golf/11">11</a></div>
<div class="golfcoursebutton"><a href="#golf/12">12</a></div>
</div>

<div style="display: flex;">
<div class="golfcoursebutton"><a href="#golf/13">13</a></div>
<div class="golfcoursebutton"><a href="#golf/14">14</a></div>
<div class="golfcoursebutton"><a href="#golf/15">15</a></div>
<div class="golfcoursebutton"><a href="#golf/16">16</a></div>
<div class="golfcoursebutton"><a href="#golf/17">17</a></div>
<div class="golfcoursebutton"><a href="#golf/18">18</a></div>
</div>
</div>
</div>
</div>





EDIT
UPDATED WITH SOLUTION










share|improve this question
















I've created a template part in PHP that copies a button to each slide in a carousel using fullpage.js. The template part has a hidden div that should open up navigation for each slide. Trying the code below, I can only get this button to work on the first slide. I'm thinking an iterated class name might help, but not sure why querySelectorAll wouldn't do it. Any advice appreciated...



http://www.pulsecreative-clients.com/staging/hogshead/#golf






 const clickOnMe = document.querySelectorAll(".course-button");

let clickOnMe = document.querySelectorAll(".course-button");
Array.from(clickOnMe).forEach(el =>
el.addEventListener("click", e =>
let showBox = e.currentTarget.nextElementSibling;
showBox.classList.toggle("open-nav");
);
);

.subnav-content 
position: fixed;
bottom: 15%;
z-index: 1;
box-sizing: border-box;
padding: 10px;
background-color: #000;
display: none;


.golfcoursebutton
box-sizing: content-box;
min-width: 30px;
height: 30px;
padding: 4px;
margin: 4px;
background-color: #fff;
color: #000;
text-align: center;


.open-nav
display: block;

<div id="jump-button" class="jumpbuttons-container">
<div class="subnav">
<button class="course-button">
JUMP TO <i class="fa fa-angle-up"></i>
</button>
<div class="subnav-content">
<div style="display: flex;">
<div class="golfcoursebutton"><a href="#golf/1">1</a></div>
<div class="golfcoursebutton"><a href="#golf/2">2</a></div>
<div class="golfcoursebutton"><a href="#golf/3">3</a></div>
<div class="golfcoursebutton"><a href="#golf/4">4</a></div>
<div class="golfcoursebutton"><a href="#golf/5">5</a></div>
<div class="golfcoursebutton"><a href="#golf/6">6</a></div>
</div>

<div style="display: flex;">
<div class="golfcoursebutton"><a href="#golf/7">7</a></div>
<div class="golfcoursebutton"><a href="#golf/8">8</a></div>
<div class="golfcoursebutton"><a href="#golf/9">9</a></div>
<div class="golfcoursebutton"><a href="#golf/10">10</a></div>
<div class="golfcoursebutton"><a href="#golf/11">11</a></div>
<div class="golfcoursebutton"><a href="#golf/12">12</a></div>
</div>

<div style="display: flex;">
<div class="golfcoursebutton"><a href="#golf/13">13</a></div>
<div class="golfcoursebutton"><a href="#golf/14">14</a></div>
<div class="golfcoursebutton"><a href="#golf/15">15</a></div>
<div class="golfcoursebutton"><a href="#golf/16">16</a></div>
<div class="golfcoursebutton"><a href="#golf/17">17</a></div>
<div class="golfcoursebutton"><a href="#golf/18">18</a></div>
</div>
</div>
</div>
</div>





EDIT
UPDATED WITH SOLUTION






 const clickOnMe = document.querySelectorAll(".course-button");

let clickOnMe = document.querySelectorAll(".course-button");
Array.from(clickOnMe).forEach(el =>
el.addEventListener("click", e =>
let showBox = e.currentTarget.nextElementSibling;
showBox.classList.toggle("open-nav");
);
);

.subnav-content 
position: fixed;
bottom: 15%;
z-index: 1;
box-sizing: border-box;
padding: 10px;
background-color: #000;
display: none;


.golfcoursebutton
box-sizing: content-box;
min-width: 30px;
height: 30px;
padding: 4px;
margin: 4px;
background-color: #fff;
color: #000;
text-align: center;


.open-nav
display: block;

<div id="jump-button" class="jumpbuttons-container">
<div class="subnav">
<button class="course-button">
JUMP TO <i class="fa fa-angle-up"></i>
</button>
<div class="subnav-content">
<div style="display: flex;">
<div class="golfcoursebutton"><a href="#golf/1">1</a></div>
<div class="golfcoursebutton"><a href="#golf/2">2</a></div>
<div class="golfcoursebutton"><a href="#golf/3">3</a></div>
<div class="golfcoursebutton"><a href="#golf/4">4</a></div>
<div class="golfcoursebutton"><a href="#golf/5">5</a></div>
<div class="golfcoursebutton"><a href="#golf/6">6</a></div>
</div>

<div style="display: flex;">
<div class="golfcoursebutton"><a href="#golf/7">7</a></div>
<div class="golfcoursebutton"><a href="#golf/8">8</a></div>
<div class="golfcoursebutton"><a href="#golf/9">9</a></div>
<div class="golfcoursebutton"><a href="#golf/10">10</a></div>
<div class="golfcoursebutton"><a href="#golf/11">11</a></div>
<div class="golfcoursebutton"><a href="#golf/12">12</a></div>
</div>

<div style="display: flex;">
<div class="golfcoursebutton"><a href="#golf/13">13</a></div>
<div class="golfcoursebutton"><a href="#golf/14">14</a></div>
<div class="golfcoursebutton"><a href="#golf/15">15</a></div>
<div class="golfcoursebutton"><a href="#golf/16">16</a></div>
<div class="golfcoursebutton"><a href="#golf/17">17</a></div>
<div class="golfcoursebutton"><a href="#golf/18">18</a></div>
</div>
</div>
</div>
</div>





 const clickOnMe = document.querySelectorAll(".course-button");

let clickOnMe = document.querySelectorAll(".course-button");
Array.from(clickOnMe).forEach(el =>
el.addEventListener("click", e =>
let showBox = e.currentTarget.nextElementSibling;
showBox.classList.toggle("open-nav");
);
);

.subnav-content 
position: fixed;
bottom: 15%;
z-index: 1;
box-sizing: border-box;
padding: 10px;
background-color: #000;
display: none;


.golfcoursebutton
box-sizing: content-box;
min-width: 30px;
height: 30px;
padding: 4px;
margin: 4px;
background-color: #fff;
color: #000;
text-align: center;


.open-nav
display: block;

<div id="jump-button" class="jumpbuttons-container">
<div class="subnav">
<button class="course-button">
JUMP TO <i class="fa fa-angle-up"></i>
</button>
<div class="subnav-content">
<div style="display: flex;">
<div class="golfcoursebutton"><a href="#golf/1">1</a></div>
<div class="golfcoursebutton"><a href="#golf/2">2</a></div>
<div class="golfcoursebutton"><a href="#golf/3">3</a></div>
<div class="golfcoursebutton"><a href="#golf/4">4</a></div>
<div class="golfcoursebutton"><a href="#golf/5">5</a></div>
<div class="golfcoursebutton"><a href="#golf/6">6</a></div>
</div>

<div style="display: flex;">
<div class="golfcoursebutton"><a href="#golf/7">7</a></div>
<div class="golfcoursebutton"><a href="#golf/8">8</a></div>
<div class="golfcoursebutton"><a href="#golf/9">9</a></div>
<div class="golfcoursebutton"><a href="#golf/10">10</a></div>
<div class="golfcoursebutton"><a href="#golf/11">11</a></div>
<div class="golfcoursebutton"><a href="#golf/12">12</a></div>
</div>

<div style="display: flex;">
<div class="golfcoursebutton"><a href="#golf/13">13</a></div>
<div class="golfcoursebutton"><a href="#golf/14">14</a></div>
<div class="golfcoursebutton"><a href="#golf/15">15</a></div>
<div class="golfcoursebutton"><a href="#golf/16">16</a></div>
<div class="golfcoursebutton"><a href="#golf/17">17</a></div>
<div class="golfcoursebutton"><a href="#golf/18">18</a></div>
</div>
</div>
</div>
</div>






javascript html css3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 11 at 20:30







rkDev

















asked Mar 8 at 22:00









rkDevrkDev

577




577












  • convert it to an array const clickOnMe = Array.from(document.querySelectorAll(".course-button"))

    – Get Off My Lawn
    Mar 8 at 22:01

















  • convert it to an array const clickOnMe = Array.from(document.querySelectorAll(".course-button"))

    – Get Off My Lawn
    Mar 8 at 22:01
















convert it to an array const clickOnMe = Array.from(document.querySelectorAll(".course-button"))

– Get Off My Lawn
Mar 8 at 22:01





convert it to an array const clickOnMe = Array.from(document.querySelectorAll(".course-button"))

– Get Off My Lawn
Mar 8 at 22:01












4 Answers
4






active

oldest

votes


















1















I've created a template part in PHP that copies a button to each slide in a carousel using fullpage.js.




Unfortunately, your code re-use is throwing an error. If we look at your source, we see:



div.jump-button
button.course-button
div.subnav-content

<script>
const clickOnMe = ...
</script>


This is repeated ~20 times.



The issue is that const can only be declared once. After that, JS will throw an error. In fact, if we view the console, we see just that:



enter image description here



Basically, after the first declaration of const clickOnMe, an error is thrown after. That's why only the first one works. I would look into moving (and consolidating) the <script> where you define clickOnMe to the bottom and invoke that once all the HTML is loaded.



Edit:



Regarding your comment, I see what you're referring to. You're now querying all the elements correctly by moving the event binding to the bottom (awesome!), but your event listener will need to be updated as well. The change is actually answered here (by @jeyko-caicedo) by referring to the event object when toggling the classList.



A more complete answer would be that you need to reference the event object (to reference the clicked element) and then query the sibling subnav-content. One way is what jeyko suggested (via closure of the forEach). The other is in the event handler with either: 1 walking up the DOM tree (using e.currentTarget.parentNode) or 2: just reference the element directly like e.currentTarget.nextElementSibling.



let clickOnMe = document.querySelectorAll(".course-button"); 
Array.from(clickOnMe).forEach(function(el) // updated `e` to `el`
el.addEventListener("click", (e) =>
let showBox = e.currentTarget.nextElementSibling;
showBox.classList.toggle("open-nav");
);
);





share|improve this answer




















  • 1





    f-nnnn hell! How did I not think of that? You're totally right, been a long week. Thank you.

    – rkDev
    Mar 9 at 0:59











  • While I was really hoping that was the issue, that didn't solve it.

    – rkDev
    Mar 9 at 1:13











  • @rkDev - You're right! I've updated my answer! :)

    – Jack
    Mar 11 at 18:25






  • 1





    And it works! Thank you Jack, I owe you a virtual beer .

    – rkDev
    Mar 11 at 18:50











  • @rkDev - Awesome, that's great to hear!! Nice site, btw! Cheers! 🍻

    – Jack
    Mar 11 at 18:54


















2














querySelectorAll does not returns an array, so it doesn't have a forEach. Luckily, you can easily create an array out of it with Array.from:



// change this
clickOnMe.forEach(...
// to this
Array.from(clickOnMe).forEach(...





share|improve this answer























  • This is unreadable :)

    – Nino Filiu
    Mar 11 at 20:24


















2














You're selecting the first of the document



document.querySelector(".subnav-content");


when it should be the first of the element



e.querySelector(".subnav-content");





share|improve this answer






























    1














    querySelectorAll returns a NodeList. It's essentially just an Array of DOM Nodes, but it doesn't support all of the Array methods that you'd expect it to have. You'll have to cast it to an array first, or manually for loop over it instead of using forEach.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071590%2fqueryselectorall-with-foreach-function-not-working-for-toggle-button-on-carousel%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1















      I've created a template part in PHP that copies a button to each slide in a carousel using fullpage.js.




      Unfortunately, your code re-use is throwing an error. If we look at your source, we see:



      div.jump-button
      button.course-button
      div.subnav-content

      <script>
      const clickOnMe = ...
      </script>


      This is repeated ~20 times.



      The issue is that const can only be declared once. After that, JS will throw an error. In fact, if we view the console, we see just that:



      enter image description here



      Basically, after the first declaration of const clickOnMe, an error is thrown after. That's why only the first one works. I would look into moving (and consolidating) the <script> where you define clickOnMe to the bottom and invoke that once all the HTML is loaded.



      Edit:



      Regarding your comment, I see what you're referring to. You're now querying all the elements correctly by moving the event binding to the bottom (awesome!), but your event listener will need to be updated as well. The change is actually answered here (by @jeyko-caicedo) by referring to the event object when toggling the classList.



      A more complete answer would be that you need to reference the event object (to reference the clicked element) and then query the sibling subnav-content. One way is what jeyko suggested (via closure of the forEach). The other is in the event handler with either: 1 walking up the DOM tree (using e.currentTarget.parentNode) or 2: just reference the element directly like e.currentTarget.nextElementSibling.



      let clickOnMe = document.querySelectorAll(".course-button"); 
      Array.from(clickOnMe).forEach(function(el) // updated `e` to `el`
      el.addEventListener("click", (e) =>
      let showBox = e.currentTarget.nextElementSibling;
      showBox.classList.toggle("open-nav");
      );
      );





      share|improve this answer




















      • 1





        f-nnnn hell! How did I not think of that? You're totally right, been a long week. Thank you.

        – rkDev
        Mar 9 at 0:59











      • While I was really hoping that was the issue, that didn't solve it.

        – rkDev
        Mar 9 at 1:13











      • @rkDev - You're right! I've updated my answer! :)

        – Jack
        Mar 11 at 18:25






      • 1





        And it works! Thank you Jack, I owe you a virtual beer .

        – rkDev
        Mar 11 at 18:50











      • @rkDev - Awesome, that's great to hear!! Nice site, btw! Cheers! 🍻

        – Jack
        Mar 11 at 18:54















      1















      I've created a template part in PHP that copies a button to each slide in a carousel using fullpage.js.




      Unfortunately, your code re-use is throwing an error. If we look at your source, we see:



      div.jump-button
      button.course-button
      div.subnav-content

      <script>
      const clickOnMe = ...
      </script>


      This is repeated ~20 times.



      The issue is that const can only be declared once. After that, JS will throw an error. In fact, if we view the console, we see just that:



      enter image description here



      Basically, after the first declaration of const clickOnMe, an error is thrown after. That's why only the first one works. I would look into moving (and consolidating) the <script> where you define clickOnMe to the bottom and invoke that once all the HTML is loaded.



      Edit:



      Regarding your comment, I see what you're referring to. You're now querying all the elements correctly by moving the event binding to the bottom (awesome!), but your event listener will need to be updated as well. The change is actually answered here (by @jeyko-caicedo) by referring to the event object when toggling the classList.



      A more complete answer would be that you need to reference the event object (to reference the clicked element) and then query the sibling subnav-content. One way is what jeyko suggested (via closure of the forEach). The other is in the event handler with either: 1 walking up the DOM tree (using e.currentTarget.parentNode) or 2: just reference the element directly like e.currentTarget.nextElementSibling.



      let clickOnMe = document.querySelectorAll(".course-button"); 
      Array.from(clickOnMe).forEach(function(el) // updated `e` to `el`
      el.addEventListener("click", (e) =>
      let showBox = e.currentTarget.nextElementSibling;
      showBox.classList.toggle("open-nav");
      );
      );





      share|improve this answer




















      • 1





        f-nnnn hell! How did I not think of that? You're totally right, been a long week. Thank you.

        – rkDev
        Mar 9 at 0:59











      • While I was really hoping that was the issue, that didn't solve it.

        – rkDev
        Mar 9 at 1:13











      • @rkDev - You're right! I've updated my answer! :)

        – Jack
        Mar 11 at 18:25






      • 1





        And it works! Thank you Jack, I owe you a virtual beer .

        – rkDev
        Mar 11 at 18:50











      • @rkDev - Awesome, that's great to hear!! Nice site, btw! Cheers! 🍻

        – Jack
        Mar 11 at 18:54













      1












      1








      1








      I've created a template part in PHP that copies a button to each slide in a carousel using fullpage.js.




      Unfortunately, your code re-use is throwing an error. If we look at your source, we see:



      div.jump-button
      button.course-button
      div.subnav-content

      <script>
      const clickOnMe = ...
      </script>


      This is repeated ~20 times.



      The issue is that const can only be declared once. After that, JS will throw an error. In fact, if we view the console, we see just that:



      enter image description here



      Basically, after the first declaration of const clickOnMe, an error is thrown after. That's why only the first one works. I would look into moving (and consolidating) the <script> where you define clickOnMe to the bottom and invoke that once all the HTML is loaded.



      Edit:



      Regarding your comment, I see what you're referring to. You're now querying all the elements correctly by moving the event binding to the bottom (awesome!), but your event listener will need to be updated as well. The change is actually answered here (by @jeyko-caicedo) by referring to the event object when toggling the classList.



      A more complete answer would be that you need to reference the event object (to reference the clicked element) and then query the sibling subnav-content. One way is what jeyko suggested (via closure of the forEach). The other is in the event handler with either: 1 walking up the DOM tree (using e.currentTarget.parentNode) or 2: just reference the element directly like e.currentTarget.nextElementSibling.



      let clickOnMe = document.querySelectorAll(".course-button"); 
      Array.from(clickOnMe).forEach(function(el) // updated `e` to `el`
      el.addEventListener("click", (e) =>
      let showBox = e.currentTarget.nextElementSibling;
      showBox.classList.toggle("open-nav");
      );
      );





      share|improve this answer
















      I've created a template part in PHP that copies a button to each slide in a carousel using fullpage.js.




      Unfortunately, your code re-use is throwing an error. If we look at your source, we see:



      div.jump-button
      button.course-button
      div.subnav-content

      <script>
      const clickOnMe = ...
      </script>


      This is repeated ~20 times.



      The issue is that const can only be declared once. After that, JS will throw an error. In fact, if we view the console, we see just that:



      enter image description here



      Basically, after the first declaration of const clickOnMe, an error is thrown after. That's why only the first one works. I would look into moving (and consolidating) the <script> where you define clickOnMe to the bottom and invoke that once all the HTML is loaded.



      Edit:



      Regarding your comment, I see what you're referring to. You're now querying all the elements correctly by moving the event binding to the bottom (awesome!), but your event listener will need to be updated as well. The change is actually answered here (by @jeyko-caicedo) by referring to the event object when toggling the classList.



      A more complete answer would be that you need to reference the event object (to reference the clicked element) and then query the sibling subnav-content. One way is what jeyko suggested (via closure of the forEach). The other is in the event handler with either: 1 walking up the DOM tree (using e.currentTarget.parentNode) or 2: just reference the element directly like e.currentTarget.nextElementSibling.



      let clickOnMe = document.querySelectorAll(".course-button"); 
      Array.from(clickOnMe).forEach(function(el) // updated `e` to `el`
      el.addEventListener("click", (e) =>
      let showBox = e.currentTarget.nextElementSibling;
      showBox.classList.toggle("open-nav");
      );
      );






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 11 at 18:57

























      answered Mar 9 at 0:33









      JackJack

      7,35021835




      7,35021835







      • 1





        f-nnnn hell! How did I not think of that? You're totally right, been a long week. Thank you.

        – rkDev
        Mar 9 at 0:59











      • While I was really hoping that was the issue, that didn't solve it.

        – rkDev
        Mar 9 at 1:13











      • @rkDev - You're right! I've updated my answer! :)

        – Jack
        Mar 11 at 18:25






      • 1





        And it works! Thank you Jack, I owe you a virtual beer .

        – rkDev
        Mar 11 at 18:50











      • @rkDev - Awesome, that's great to hear!! Nice site, btw! Cheers! 🍻

        – Jack
        Mar 11 at 18:54












      • 1





        f-nnnn hell! How did I not think of that? You're totally right, been a long week. Thank you.

        – rkDev
        Mar 9 at 0:59











      • While I was really hoping that was the issue, that didn't solve it.

        – rkDev
        Mar 9 at 1:13











      • @rkDev - You're right! I've updated my answer! :)

        – Jack
        Mar 11 at 18:25






      • 1





        And it works! Thank you Jack, I owe you a virtual beer .

        – rkDev
        Mar 11 at 18:50











      • @rkDev - Awesome, that's great to hear!! Nice site, btw! Cheers! 🍻

        – Jack
        Mar 11 at 18:54







      1




      1





      f-nnnn hell! How did I not think of that? You're totally right, been a long week. Thank you.

      – rkDev
      Mar 9 at 0:59





      f-nnnn hell! How did I not think of that? You're totally right, been a long week. Thank you.

      – rkDev
      Mar 9 at 0:59













      While I was really hoping that was the issue, that didn't solve it.

      – rkDev
      Mar 9 at 1:13





      While I was really hoping that was the issue, that didn't solve it.

      – rkDev
      Mar 9 at 1:13













      @rkDev - You're right! I've updated my answer! :)

      – Jack
      Mar 11 at 18:25





      @rkDev - You're right! I've updated my answer! :)

      – Jack
      Mar 11 at 18:25




      1




      1





      And it works! Thank you Jack, I owe you a virtual beer .

      – rkDev
      Mar 11 at 18:50





      And it works! Thank you Jack, I owe you a virtual beer .

      – rkDev
      Mar 11 at 18:50













      @rkDev - Awesome, that's great to hear!! Nice site, btw! Cheers! 🍻

      – Jack
      Mar 11 at 18:54





      @rkDev - Awesome, that's great to hear!! Nice site, btw! Cheers! 🍻

      – Jack
      Mar 11 at 18:54













      2














      querySelectorAll does not returns an array, so it doesn't have a forEach. Luckily, you can easily create an array out of it with Array.from:



      // change this
      clickOnMe.forEach(...
      // to this
      Array.from(clickOnMe).forEach(...





      share|improve this answer























      • This is unreadable :)

        – Nino Filiu
        Mar 11 at 20:24















      2














      querySelectorAll does not returns an array, so it doesn't have a forEach. Luckily, you can easily create an array out of it with Array.from:



      // change this
      clickOnMe.forEach(...
      // to this
      Array.from(clickOnMe).forEach(...





      share|improve this answer























      • This is unreadable :)

        – Nino Filiu
        Mar 11 at 20:24













      2












      2








      2







      querySelectorAll does not returns an array, so it doesn't have a forEach. Luckily, you can easily create an array out of it with Array.from:



      // change this
      clickOnMe.forEach(...
      // to this
      Array.from(clickOnMe).forEach(...





      share|improve this answer













      querySelectorAll does not returns an array, so it doesn't have a forEach. Luckily, you can easily create an array out of it with Array.from:



      // change this
      clickOnMe.forEach(...
      // to this
      Array.from(clickOnMe).forEach(...






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Mar 8 at 22:05









      Nino FiliuNino Filiu

      3,26341531




      3,26341531












      • This is unreadable :)

        – Nino Filiu
        Mar 11 at 20:24

















      • This is unreadable :)

        – Nino Filiu
        Mar 11 at 20:24
















      This is unreadable :)

      – Nino Filiu
      Mar 11 at 20:24





      This is unreadable :)

      – Nino Filiu
      Mar 11 at 20:24











      2














      You're selecting the first of the document



      document.querySelector(".subnav-content");


      when it should be the first of the element



      e.querySelector(".subnav-content");





      share|improve this answer



























        2














        You're selecting the first of the document



        document.querySelector(".subnav-content");


        when it should be the first of the element



        e.querySelector(".subnav-content");





        share|improve this answer

























          2












          2








          2







          You're selecting the first of the document



          document.querySelector(".subnav-content");


          when it should be the first of the element



          e.querySelector(".subnav-content");





          share|improve this answer













          You're selecting the first of the document



          document.querySelector(".subnav-content");


          when it should be the first of the element



          e.querySelector(".subnav-content");






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 8 at 22:20









          Jeyko CaicedoJeyko Caicedo

          313




          313





















              1














              querySelectorAll returns a NodeList. It's essentially just an Array of DOM Nodes, but it doesn't support all of the Array methods that you'd expect it to have. You'll have to cast it to an array first, or manually for loop over it instead of using forEach.






              share|improve this answer



























                1














                querySelectorAll returns a NodeList. It's essentially just an Array of DOM Nodes, but it doesn't support all of the Array methods that you'd expect it to have. You'll have to cast it to an array first, or manually for loop over it instead of using forEach.






                share|improve this answer

























                  1












                  1








                  1







                  querySelectorAll returns a NodeList. It's essentially just an Array of DOM Nodes, but it doesn't support all of the Array methods that you'd expect it to have. You'll have to cast it to an array first, or manually for loop over it instead of using forEach.






                  share|improve this answer













                  querySelectorAll returns a NodeList. It's essentially just an Array of DOM Nodes, but it doesn't support all of the Array methods that you'd expect it to have. You'll have to cast it to an array first, or manually for loop over it instead of using forEach.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 8 at 22:14









                  Cory DanielsonCory Danielson

                  10.2k23342




                  10.2k23342



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071590%2fqueryselectorall-with-foreach-function-not-working-for-toggle-button-on-carousel%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      1928 у кіно

                      Захаров Федір Захарович

                      Ель Греко