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

114
Views
I can't link to a new page with active class by JavaScript on PHP

I'm a new learner on PHP. I can't solve any problem on my code.

In sidebar.php

<nav class="side-bar">
        <ul>
            <li>
                <a href="index.php" class="category active">
                    <i class="fi fi-bs-home"></i>
                    <span>Home</span>
                </a>
            </li>
            <li>
                <a href="#" class="category">
                    <i class="fi fi-bs-apps"></i>
                    <span>Room</span>
                </a>
            </li>
            <li>
                <a href="contact.php" class="category">
                    <i class="fi fi-br-comment-alt"></i>
                    <span>Contact</span>
                </a>
            </li>
            </ul>
</nav>

In script.js

const category = document.querySelectorAll('.category');

category.forEach(cate =>{
    cate.addEventListener('click',()=>{
        removeClass();
        cate.classList.add('active');
    })
})

function removeClass(){
    category.forEach(cate=>{
        cate.classList.remove('active');
    })
}

In index.php

#code
<?php include ("sidebar.php"); ?>
#code

but the active class is not highlighted when I link to a new page, but it works if I use href="#" in tag. How can I solve this problem?

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

0

You need to define the colors you want to use when an item is active and when an item is visited, but not active. You may choose the colors you prefer instead of the ones I have chosen.

const category = document.querySelectorAll('.category');

category.forEach(cate =>{
    cate.addEventListener('click',()=>{
        removeClass();
        cate.classList.add('active');
    })
})

function removeClass(){
    category.forEach(cate=>{
        cate.classList.remove('active');
    })
}
a.active {
    color: #7F7FEE;
}

a, a:not(.active):visited {
    color: #0000FF;
}
<nav class="side-bar">
        <ul>
            <li>
                <a href="index.php" class="category active">
                    <i class="fi fi-bs-home"></i>
                    <span>Home</span>
                </a>
            </li>
            <li>
                <a href="#" class="category">
                    <i class="fi fi-bs-apps"></i>
                    <span>Room</span>
                </a>
            </li>
            <li>
                <a href="contact.php" class="category">
                    <i class="fi fi-br-comment-alt"></i>
                    <span>Contact</span>
                </a>
            </li>
            </ul>
</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!