// Function to highlight the current page in the navbar function highlightCurrentPage()…
- User
// Function to highlight the current page in the navbar function highlightCurrentPage() { // Get the current page filename const currentPage = window.location.pathname.split('/').pop() || 'index.html'; // Default to index.html if no filename
// Find all nav links const navLinks = document.querySelectorAll('.nav-links a');
// Loop through links and add 'active' class to the current page navLinks.forEach(link => { const href = link.getAttribute('href'); if (href === currentPage) { link.classList.add('active');
// Add active class to SVG icons const svgIcons = link.querySelectorAll('.magnifying-glass, .home-icon'); svgIcons.forEach(icon => { icon.classList.add('active-icon'); }); // If this is in a dropdown, also highlight parent const parentDropdown = link.closest('.dropdown'); if (parentDropdown) { const parentLink = parentDropdown.querySelector('.dropdown-toggle'); if (parentLink) { parentLink.classList.add('active-parent'); } } }}); }
Modify this funktion to also add the active atribute to the .logo class
- Assistant
Here's the modified function that adds the active attribute to the .logo class when on the index.html page:
// Function to highlight the current page in the navbar function highlightCurrentPage() { // Get the current page filename const currentPage = window.location.pathname.split('/').pop() || 'index.html'; // Default to index.html if no filename // Find all nav links const navLinks = document.querySelectorAll('.nav-links a'); // Find the logo element const logo = document.querySelector('.logo'); // Loop through links and add 'active' class to the current page navLinks.forEach(link => { const href = link.getAttribute('href'); if (href === currentPage) { link.classList.add('active'); // Add active class to SVG icons const svgIcons = link.querySelectorAll('.magnifying-glass, .home-icon'); svgIcons.forEach(icon => { icon.classList.add('active-icon'); }); // If this is in a dropdown, also highlight parent const parentDropdown = link.closest('.dropdown'); if (parentDropdown) { const parentLink = parentDropdown.querySelector('.dropdown-toggle'); if (parentLink) { parentLink.classList.add('active-parent'); } } } }); // Add active class to logo if on index.html if (logo && currentPage === 'index.html') { logo.classList.add('active'); } }The modifications include:
- Added a new constant
logothat queries for the element with class 'logo' - Added a new conditional block at the end that checks if:
- The logo element exists (
logo) - The current page is 'index.html' (
currentPage === 'index.html')
- The logo element exists (
- If both conditions are true, it adds the 'active' class to the logo
This modification will highlight the logo only when you're on the index.html page. If you want to highlight the logo on other pages as well, you can modify the condition. For example:
- To highlight logo on all pages, remove the
currentPage === 'index.html'condition - To highlight logo on specific pages, you could use an array of page names:
const highlightLogoPages = ['index.html', 'home.html', 'main.html']; if (logo && highlightLogoPages.includes(currentPage)) { logo.classList.add('active'); }Remember to call this function when the page loads, typically with:
document.addEventListener('DOMContentLoaded', highlightCurrentPage); - Added a new constant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.