Adjust the color of the active link on the page using Angular and CSS

I have a project that I need to modify by adding a sub menu that appears on every page but is only coded once. My goal is to highlight the link for the current page, all within one HTML snippet. Although the list renders correctly, I'm struggling to get the current page to highlight. While I've seen suggestions about adding 'current link' formatting in a separate file, I must include all the code in this snippet. This task requires the use of HTML, CSS, and Angular, and I am new to learning Angular.

Given these constraints, is it possible to achieve what I am trying to do? If so, how can I make it work?

body {
  font-family: Source Sans Pro;
}

ul {
  float: left;
  width: 100%;
  padding: 0;
  margin: 0;
  list-style-type: none;
}

a {
  float: left;
  width: 6em;
  text-decoration: none;
  padding: 0.2em 0.6em;
  border-right: 1px solid white;

}

#navbar li a.current {
  background-color: #FFF;
}

li {
  display: inline;
}

#bl {
  background-color: #5a5a5a;
  color: yellow;
}

#or {
  background-color: #5a5a5a;
  color: yellow;
}

#gr {
  background-color: #5a5a5a;
  color: yellow;
}

#bl:hover, #or:hover, #gr:hover {
  background-color: #ff6900;
}

.left {
  float: left;
}
 
<body ng-app="watch"> 
  <div ng-controller="control"> 
    <ul>
      <li><a href="#" ng-click = "person()" id="bl">Person</a></li>
      <li><a href="#" ng-click = "product()" id="or">Product</a></li>
      <li><a href="#" ng-click = "place()" id="gr">Place</a></li>
    </ul>
  </div>
</body>
my controller look like this

'use strict';
var mymodule = angular.module("watch", []);

mymodule.controller("control",function($scope) {
    $scope.jsonerator = true;
    $scope.jsonerator2 = false;
$scope.jsonerator3 = false;


    $scope.person = function() {
        $scope.jsonerator = true;
        $scope.jsonerator2 = false;
$scope.jsonerator3 = false;

 }


 $scope.product = function() {
        $scope.jsonerator = false;
        $scope.jsonerator2 = true;
$scope.jsonerator3 = false;

}


 $scope.place = function() {
        $scope.jsonerator = false;
        $scope.jsonerator2 = false;
$scope.jsonerator3 = true;
   }
};

and this is my menu on jsfiddle:jsfiddle

Thanks

Answer №1

If you're looking to streamline your code, consider transitioning from CSS IDs to classes and possibly removing IDs from your anchor tags. Another approach could be utilizing ng-class; it automatically adds a class based on a boolean condition. In this scenario, we are referencing the variables in your jsonerator $scope.

<li><a href="#" ng-click = "person()" id="bl" ng-class="{bl: jsonerator}">Person</a></li>
<li><a href="#" ng-click = "product()" id="or" ng-class="{or: jsonerator2}">Product</a></li>
<li><a href="#" ng-click = "place()" id="gr" ng-class="{gr: jsonerator3}">Place</a></li>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

PHP-powered webcasting platform

I have a client who is looking for a website that can live stream uploaded videos through search functionality. One of their main requests is to cover and record certain weekly events on camera, with the video being visible live on the website. While I am ...

What is the best way to manage the connections in my D3 tree chart?

I've been working on customizing a tool from an open source library called angular-d3-tree, but I'm struggling with getting the links to connect properly in my D3 tree structure. This is what my current tree layout looks like: https://i.stack.im ...

Share JSON data across functions by calling a function

I am currently working on a project where I need to load JSON using a JavaScript function and then make the loaded JSON objects accessible to other functions in the same namespace. However, I have encountered some difficulties in achieving this. Even after ...

Guide on how to return a JSON result as a Dictionary instead of an Array using Express.js

I need to format my response as a dictionary with specific key-value pairs, like so: { "id": 5928101, "category": "animal welfare", "organizer": "Adam", "title": "Cat Cabaret& ...

Creating a stylish navigation bar with custom components using Material UI and React

I've been experimenting with the BottomNavigation component from Material UI, but instead of using labels and text, I want to incorporate custom-made ImageButton components. Here's the code snippet from Material UI: import React from 'rea ...

extract information from a document and store it in an array

As I delve into the realm of programming, I find myself grappling with the best approach to extract data from a file and store it in an array. My ultimate aim is to establish a dictionary for a game that can verify words provided by players. Despite my no ...

Inquiry on integrating Spotify with Axios for my debut solo project (beginner inquiry)

I have a question regarding my first solo project in React. I started learning code in September and I'm facing an issue while making a POST request to the Spotify API to retrieve an access token: Despite following the recommended 'Content-Type& ...

Employing PHP conditions alongside JavaScript to conceal the div elements

I have been struggling with a problem for the past few hours and still haven't found a solution. I am new to php. What I am trying to achieve is to display and hide a div based on a PHP condition. Here is the scenario: Please note: By default, the DI ...

Styling Challenge: Making the button inside a form match the design of the button outside the form

I have noticed that the button inside a form has a lot more padding around it compared to the one outside of the form. I am trying to adjust the within-form button so that it matches the padding of the without-form button. HTML/PHP <?php if (! ...

Scroll to the top of a new page with Material UI's table component

Feeling a little stuck here. I've tested various settings with scrollTop, but haven't had much luck. To clarify, I'm working with Material UI and their pagination features in conjunction with a table (you can find the documentation here). W ...

The element's height appears to be fluctuating unexpectedly when I attempt to adjust it using percentage values within a narrow range

I'm utilizing React and Bootstrap in this project. Here's an overview of my code: I have an element with height set to 0, in rem. My goal is to make the height of this element increase as I scroll down the page, creating the illusion that it is ...

Selecting CSS Properties with jQuery

I am trying to make an image change color to blue and also change the background color to blue when it is clicked inside a div. However, my code doesn't seem to be working as expected. Is there a more efficient way to apply CSS to elements? FIDDLE v ...

Guide to sending a response to an AJAX post request in Express with Node.js: Answered

For a project focused on practicing Node.js and jQuery Ajax, I'm working on a simple task. Essentially, I have an ajax post request that sends data to a Node.js server and waits for a response. On the server-side, there's code that processes this ...

JavaScript Linked List Operations: Issues with the removeHead() and contains() Functions

Incorporating ES6 syntax, the challenge is to construct a linked list and subject it to an npm linter test. Below is the code implementation: class LinkedList { constructor() { this.head = null; this.tail = null; // Do not alter anything wit ...

What could be the reason for the inconsistent behavior of onClick, causing it to occasionally fail to display the associated

I just started using typescript 2 days ago, mainly to create a custom component for my streamlit app. I've made a navigation bar with a tab that can be clicked on the sidebar, but it's displaying some erratic behavior. Sometimes when I click on t ...

Having trouble triggering a click event with JQuery

I have a hidden link for downloading Audio Files that I want the user to access using a button. However, I am having trouble triggering the click event. Below is the HTML code: <a class="APopupDown" data-icon="home" id="DownloadFile" href="http://yaho ...

Show the outcome of an HTTP POST request exclusively for the row that corresponds to the button being clicked in AngularJS

As a newcomer to Angular, I am currently working on starting and stopping a Windows service in .Net using AngularJS. The user interface includes fields for server name, the status of the Windows service, and two buttons to start and stop the service. Whe ...

Uniting the client-side jQuery and server-side Express for enhanced functionality

Currently, I am deepening my understanding of Express/Node. My goal is to construct a basic HTML form that undergoes client-side validation (using jQuery/AJAX) while also being processed server-side (via Express.js). The application's complexity remai ...

Significant Google Maps malfunction detected with API V3

Update: Issue resolved, check out my answer below. The scenario: User interacts with a map-image google maps API V3 is loaded using ajax the map is displayed in a dialog window or lightbox What's going on: The map loads and all features are funct ...

Challenges with incorporating numerous text boxes in real time

There are two text boxes side by side with a "+" sign next to each. When the plus sign is clicked, a new text box appears with both the "+" and "-" signs for adding and removing the text box. I followed instructions from this resource to create my text box ...