How to ensure only one tab is coloured upon clicking?

github292929
:

I'm trying to write a function such that when you click a tab (created using bootstrap) it changes to a gold color. This bit works fine, however, I want to toggle the active property of each tab (bootstrap) so that it's on to give a nice white outline. The issue is unless you click on the same tab again, it stays white. Any ideas? Thanks.

This is my work so far:

let timesClicked = 1;

let navItems = document.querySelectorAll('.changeColor');
navItems.forEach(item => {
  item.addEventListener("click", () => {
    timesClicked++;
    console.log(timesClicked);
    let previousItem = item;
    if (timesClicked % 2 == 0) {
      item.style.color = "#f0a500";
      item.classList.toggle('active');
      console.log(item);
    } else {
      previousItem.classList.toggle('active');
      console.log(previousItem);
    };

    navItems.forEach(otherItem => {
      if (otherItem !== item) {
        otherItem.style.color = "#1A1C20";
        /*
        item.addEventListener("click", () =>
          item.classList.toggle('active'));
       ;*/
      };
    });

  });
})
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

<ul class="nav nav-tabs nav-fill justify-content-center mt-3" id="myTab" role="tablist">
  <li class="nav-item" role="presentation">
    <a class="nav-link changeColor active" id="mathematics-tab" data-toggle="tab" href="#Mathematics" role="tab"
      aria-controls="Mathematics" aria-selected="true">Mathematics</a>
  </li>
  <li class="nav-item" role="presentation">
    <a class="nav-link changeColor black-text-start" id="english-tab" data-toggle="tab" href="#profile" role="tab"
      aria-controls="profile" aria-selected="false">English</a>
  </li>
  <li class="nav-item" role="presentation">
    <a class="nav-link changeColor black-text-start" id="verbal-tab" data-toggle="tab" href="#contact" role="tab"
      aria-controls="contact" aria-selected="false">Verbal</a>
  </li>
  <li class="nav-item" role="presentation">
    <a class="nav-link changeColor black-text-start" id="nonverbal-tab" data-toggle="tab" href="#contact" role="tab"
      aria-controls="contact" aria-selected="false">Nonverbal</a>
  </li>
</ul>

Always Helping
:

You are over complicating your code this can simply be done using querySelectorAll and forEach method.

Just remove the active class and color from all the nav items and then add only to the clicked menu item only using Event.target method

Simplified code Demo:

let navItems = document.querySelectorAll('.changeColor');
navItems.forEach(item => {
  item.addEventListener("click", (e) => {
    navItems.forEach(otherItem => {
      otherItem.style.color = "#1A1C20";
      otherItem.classList.remove('active');
    });
    e.target.classList.add('active');
    e.target.style.color = "#f0a500";
  });
})
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

<ul class="nav nav-tabs nav-fill justify-content-center mt-3" id="myTab" role="tablist">
  <li class="nav-item" role="presentation">
    <a class="nav-link changeColor active" id="mathematics-tab" data-toggle="tab" href="#Mathematics" role="tab" aria-controls="Mathematics" aria-selected="true">Mathematics</a>
  </li>
  <li class="nav-item" role="presentation">
    <a class="nav-link changeColor black-text-start" id="english-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="false">English</a>
  </li>
  <li class="nav-item" role="presentation">
    <a class="nav-link changeColor black-text-start" id="verbal-tab" data-toggle="tab" href="#contact" role="tab" aria-controls="contact" aria-selected="false">Verbal</a>
  </li>
  <li class="nav-item" role="presentation">
    <a class="nav-link changeColor black-text-start" id="nonverbal-tab" data-toggle="tab" href="#contact" role="tab" aria-controls="contact" aria-selected="false">Nonverbal</a>
  </li>
</ul>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I change one image to another upon clicking on a button

From Dev

How to ensure object has only one thread

From Dev

How to ensure that two programs always run ? (where the one depends upon the other.)

From Dev

How can I enable one specific tab by clicking the button?

From Java

How to ensure only one Azure Function BlobTrigger runs at a time?

From Dev

How can I ensure that only one if a kind of Jenkins job is run?

From Dev

How does Akka ensure actors process only one message at a time

From Dev

How do I ensure only one instance of the object of the class is created?

From Dev

How can I ensure that only one if a kind of Jenkins job is run?

From Dev

How to ensure only one request at the same time in Java

From Dev

How to open HTML in only one Window/Tab

From Dev

How to have Firebug only open in one tab

From Dev

How to toggle an animation upon clicking a button

From Dev

How to add a border to a cell upon clicking

From Dev

How to uncheck radio button upon double clicking?

From Dev

How to toggle an animation upon clicking a button

From Dev

How to uncheck radio button upon double clicking?

From Dev

How do I close this JOptionPane upon clicking?

From Dev

how to make a form disappear upon clicking submit?

From Dev

How to get button's id upon clicking it?

From Dev

How to navigate from one tab to another tab by clicking a link or url in IONIC?

From Dev

Check more than one checkbox upon clicking a button c#

From Dev

How to activate a function on clicking a tab?

From Dev

How do I ensure there is only one instance of a class at any one time in Objective-C?

From Dev

Django 1.8: How can I ensure that of Two Fields in a Model, At Least One or Only One must meet a condition?

From Dev

How to ensure only one Play instance actually runs a method at one certain moment

From Dev

How to ensure Stage opens as a window and not a tab?

From Dev

How to ensure Stage opens as a window and not a tab?

From Dev

Ensure that there is only one running PHP process on Linux

Related Related

  1. 1

    How do I change one image to another upon clicking on a button

  2. 2

    How to ensure object has only one thread

  3. 3

    How to ensure that two programs always run ? (where the one depends upon the other.)

  4. 4

    How can I enable one specific tab by clicking the button?

  5. 5

    How to ensure only one Azure Function BlobTrigger runs at a time?

  6. 6

    How can I ensure that only one if a kind of Jenkins job is run?

  7. 7

    How does Akka ensure actors process only one message at a time

  8. 8

    How do I ensure only one instance of the object of the class is created?

  9. 9

    How can I ensure that only one if a kind of Jenkins job is run?

  10. 10

    How to ensure only one request at the same time in Java

  11. 11

    How to open HTML in only one Window/Tab

  12. 12

    How to have Firebug only open in one tab

  13. 13

    How to toggle an animation upon clicking a button

  14. 14

    How to add a border to a cell upon clicking

  15. 15

    How to uncheck radio button upon double clicking?

  16. 16

    How to toggle an animation upon clicking a button

  17. 17

    How to uncheck radio button upon double clicking?

  18. 18

    How do I close this JOptionPane upon clicking?

  19. 19

    how to make a form disappear upon clicking submit?

  20. 20

    How to get button's id upon clicking it?

  21. 21

    How to navigate from one tab to another tab by clicking a link or url in IONIC?

  22. 22

    Check more than one checkbox upon clicking a button c#

  23. 23

    How to activate a function on clicking a tab?

  24. 24

    How do I ensure there is only one instance of a class at any one time in Objective-C?

  25. 25

    Django 1.8: How can I ensure that of Two Fields in a Model, At Least One or Only One must meet a condition?

  26. 26

    How to ensure only one Play instance actually runs a method at one certain moment

  27. 27

    How to ensure Stage opens as a window and not a tab?

  28. 28

    How to ensure Stage opens as a window and not a tab?

  29. 29

    Ensure that there is only one running PHP process on Linux

HotTag

Archive