Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

264
Views
JavaScript Nav Bar TypeError: 'undefined' is not an object

I am new to HTML, CSS and JavaScript. I am getting the error which states: TypeError: 'undefined' is not an object. Now I have tried to look for the issue but had no luck. Can someone help? Error occurs in the console at: item.ClassList.container('active') && handleIndicator(item);

var indicator = document.querySelector('.indicator');
var items = document.querySelectorAll('.nav-item');

function handleIndicator(el) {
  items.forEach(function(el) {
    item.ClassList.remove('active');
    item.removeAttribute('style');
  });

  indicator.style.width = "".concat(el.offsetWidth, "px");
  indicator.style.left = "".concat(el.offsetLeft, "px");
  indicator.style.backgroundColor = el.getAttribute('active-color');
  el.ClassList.add('active');
  el.style.color = el.Attributes('active-color');
}

items.forEach(function(item, index) {
  item.addEventListener('click', function(e) {
    handleIndicator(e.target);
  });
  item.ClassList.container('active') && handleIndicator(item);
});
<nav>
  <a class="nav-item active" active-color="red">Test 1</a>
  <a class="nav-item" active-color="blue">Test 2</a>
  <a class="nav-item" active-color="purple">Test 3</a>
  <a class="nav-item" active-color="pink">Test 4</a>
  <div class="indicator"></div>
</nav>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

item.ClassList.remove('active'); should be item.classList.remove('active');. it should be in lowercase

about 4 years ago · Juan Pablo Isaza Report

0

Try this:

const indicator = document.querySelector('.indicator');
const items = document.querySelectorAll('.nav-item');

function handleIndicator(el) {
  items.forEach(e => {
    e.classList.remove('active');
    e.removeAttribute('style');
  });

  indicator.style.width = "".concat(el.offsetWidth, "px");
  indicator.style.left = "".concat(el.offsetLeft, "px");
  indicator.style.backgroundColor = el.getAttribute('active-color');
  el.classList.add('active');
  el.style.color = el.getAttribute('active-color')
}

items.forEach(item => {

  item.addEventListener('click', function(e) {
    handleIndicator(e.target);
  });
  //item.classList.contains('active') && handleIndicator(item);
});
<nav>
  <a class="nav-item active" active-color="red">Test 1</a>
  <a class="nav-item" active-color="blue">Test 2</a>
  <a class="nav-item" active-color="purple">Test 3</a>
  <a class="nav-item" active-color="pink">Test 4</a>
  <div class="indicator"></div>
</nav>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!