0

The Variable slide_titles and slide_background are logging out as undefined in the Click Event Listener. Why is this?

Im trying to add and remove classes using classList. It's not working because the global variables aren't visible inside the on click EventListener.

html

 <div class="product-type">
    <ul>
        <li class="slide__title selected">Cars</li>
        <li class="slide__title" >Motorcycles</li>
        <li class="slide__title">Power Equiptment</li>
        <li class="slide__title">Marine</li>
    </ul>
  </div>

    <ul id="slides">
        <li class="slide cars"></li>
        <li class="slide bikes"></li>
        <li class="slide mower"></li>   
        <li class="slide boat"></li>
    </ul>

javascript

    var slide_titles = document.querySelectorAll('.slide__title');
var slide_background = document.querySelectorAll('.slide');


function removeSelected() {
  for(var i = 0; i < slide_titles.length; i++) {
    if(slide_titles[i].className.indexOf('selected') != -1) {
      console.log("remove");
      slide_titles[i].classList.remove("selected");
    }
  }
}


var clickEvent = function(index) {
slide_titles[i].addEventListener("click", () => {
        removeSelected();
        slide_background[i].classList.add("show");
        slide_titles[i].classList.add("selected");
        console.log(slide_background[i]);

  });
}


   for(var i = 0; i < slide_titles.length; i++) {
      clickEvent(i);
   }

here is the fiddle https://jsfiddle.net/6yjfdftc/1/

5
  • Vanilla JS please! Commented Sep 2, 2016 at 3:22
  • Try changing .slide__title and .slide to li.slide__title and li.slide Commented Sep 2, 2016 at 3:22
  • My guess is because the JavaScript is executing before the HTML had loaded. Try wrapping you code in an event handler fired off from the window.onload event Commented Sep 2, 2016 at 3:26
  • I have made those changes unfortunately the error is still occuring Commented Sep 2, 2016 at 3:30
  • Thank you for your help, this issue has been fixed Commented Sep 2, 2016 at 3:33

1 Answer 1

3

It's logging undefined because i is undefined in that scope. Since you pass i to it in your for loop, try this:

var clickEvent = function(index) {
    slide_titles[index].addEventListener("click", () => {
        removeSelected();
        slide_background[index].classList.add("show");
        slide_titles[index].classList.add("selected");
        console.log(slide_background[index]);
    });
}

Here's an updated fiddle.

Sign up to request clarification or add additional context in comments.

3 Comments

i have gone through and made those changes to the clickEvent function. nothing is logging now.
Have you checked out the fiddle? index is only used in the clickEvent function
apologies, this has fixed the issue. My mistake. Thank you for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.