The navigation tab retains a fixed color

My ASP.net project includes a master page with navigation links. When I click on a link to load a different asp page, the navigation tab does not stay in the clicked color state; it reverts back to its original color. Since all pages are similar, using CSS won't work due to the page reloading each time. Is there a way to use JavaScript to instruct the page to display the hover color when loaded and maintain it? The challenge is that I can only modify HTML on the master page, making it difficult to implement changes. I have considered using nth-child but haven't been able to make it work. For reference, here is a basic example of my code:

<div>
   <ul>
     <li>One</li>
     <li>Two</li>
     <li>Three</li>
   </ul>
</div>

How can I ensure that the 2nd link stays in its hover color upon page loading?

Answer №1

It's been some time since I dabbled in asp, so my memory might be a bit fuzzy on the exact terminology. However, I can certainly provide you with some guidance.

To start off, make sure to assign unique ids to all of your navigation links within your master page. This will streamline the process of accessing these links on your specific asp pages. Without unique ids, it can be challenging to pinpoint the exact link you want to manipulate. With jQuery commands like $("div ul li:nth-child('2')"), you may end up selecting the second li that falls under a ul inside a div, but this could occur anywhere on your page.

Assuming your navigation panel is structured as follows:

<div>
   <ul>
     <li id="linkOne">One</li>
     <li id="linkTwo">Two</li>
     <li id="linkThree">Three</li>
   </ul>
</div>

Next, in the page that loads when "Two" is clicked, you'll need to insert a script with an onLoad handler to modify the link:

<script>
  document.onload = function() { $("#linkTwo").addClass("hover"); };
</script>

This script will wait for the document to load (to prevent attempting to manipulate a non-existent element), then execute a function that locates the element with the id "linkTwo" and applies the css class "hover" to it.

Keep in mind that this code snippet will vary for each specific asp page or could potentially be generated by your server-side logic.

Answer №2

It seems like your question may be a bit unclear, but if I understand correctly, you should use a specific CSS class that is loaded when the current page is determined.

You can try something similar to this (I'm not familiar with ASP, so I'll provide a general example):

<div>
   <ul>
     <li <?php if($CurrentPage = 'One') {echo 'class="active"';} ?>>One</li>
     <li <?php if($CurrentPage = 'Two') {echo 'class="active"';} ?> >Two</li>
     <li <?php if($CurrentPage = 'Three') {echo 'class="active"';} ?>>Three</li>
   </ul>
</div>

By adding this special class to your element, it will have the hover color and solve the issue. Hopefully, you can adapt this to ASP.

Answer №3

Utilize the following:

html markup

<body>
    <div>
       <ul style="display:inline; background-color:lightgray; float:left">
         <li style="margin:20px; display:inline-block" onclick="frames['target'].location.href='one.html'; toggle_bgcolor(this);">One</li>
         <li style="margin:20px; display:inline-block" onclick="frames['target'].location.href='two.html'; toggle_bgcolor(this);">Two</li>
         <li style="margin:20px; display:inline-block" onclick="frames['target'].location.href='three.html'; toggle_bgcolor(this);">Three</li>
       </ul>
       <iframe id="target"></iframe>
    </div>
</body>

JavaScript function

function toggle_bgcolor(elem)
{
    for(var c=0; c<elem.parentNode.getElementsByTagName('li').length; c++)
    {
        elem.parentNode.getElementsByTagName('li')[c].style.backgroundColor='';
    }
    elem.style.backgroundColor='limegreen';
}

The JavaScript block for the onclick event in each <li> element will alternate the background-color of each <li> element when another <li> is clicked.

Take a look at this JSFiddle demo

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

Ways to import JavaScript into child HTMLs embedded within ng-view

I'm diving into angular for the first time and I need to figure out how to incorporate JavaScript into my HTML within ng-view. Some suggestions I came across mention adding jQuery. Can someone provide some guidance on specifically what needs to be ad ...

Exploring the functionality of jQuery by incorporating a variable into the selector

I attempted to modify the src attribute of an image file using a variable, but it did not actually change. Can anyone pinpoint where I went wrong in using the variable? jquery var p2begen = "416"; $("[id=i'" + p2begen + "']").attr("src", "check ...

What is the best way to allow users to click a Grid component in order to toggle the state of a

I have a Grid component that is part of a row within the grid container, along with other rows. This particular row consists of a description and a checkbox. How can I set it up so that clicking anywhere on the row will toggle the checkbox on and off? &l ...

Creating a React Component in TypeScript that utilizes Promise Objects for data handling

After receiving a Promise type Object as a response, an error has been encountered: Error: Objects are not valid as a React child (found: object with keys { key1, key2, key3 ...}... How can this issue be resolved? // Component.tsx import React, { us ...

Using jQuery to update a specific item in a list

My current project involves developing an Image Gallery app. It uses <img> tags within li elements. The code snippet is as follows: var $slideR_wrap = $(".slideRoller_wrapper"); var $slidesRoller = $slideR_wrap.find(".slidesRoller"); var $slideR ...

Building Dynamic Props in Vue.js using v-select Component

I am utilizing a chart that relies on properties for data. <template> <v-row> <v-col col="12" sm="12"> <Chart :data="series2"></Chart> ### This chart receives the props < ...

What is the best way to refresh the content of an iframe when a link within the iframe is clicked?

In the code below, I am resizing the iframe based on its content: <html> <head> <title></title> </head> <body> <iframe src="http://page.html" id="iframeid" width="600" height="700" scrolling="n ...

Vue.Js for a Single Page Application utilizing Two Data Sources

Currently, I am working on developing a Single Page Application using vue.js. My project consists of 2 bundles of pages stored in separate S3 buckets - one public and one private. The public bundle is meant to be accessible to all users, while the private ...

Utilizing Titanium MVC: Invoke function and pause for response

Currently, I am in the process of developing my very first Titanium iPhone application. Within a model module, I have this code snippet: (function() { main.model = {}; main.model.getAlbums = function(_args) { var loader = Titanium.Ne ...

Locating the controls adjacent to a specific element in the document

Is it possible to locate the preceding and following sibling controls within an ASP.net form via code-behind, akin to using findControl()? Sometimes assigning an ID to a control just for the purpose of locating it with parent().findControl("ID") seems red ...

Incorporating React Native elements into a native Android interface (UI element)

I am facing an issue in my react-native application where I have a native Android view (created in java) rendering correctly. However, when I attempt to include one of my react-native javascript components within it, I encounter this error: java.lang.Cla ...

ALSO, various criteria

function findLogicalAND(){ let result; let index; for (index = 0; index < arguments.length; index++){ result = arguments[index] && arguments[index+1]; } return result; } console.log(findLogicalAND(true, true, false, false)); I want to r ...

Leverage the Application variable in ASP when transitioning to ASP.NET

As I work on converting an old ASP classic application to ASP.net VB, I came across a code snippet in the classic version where an application variable is declared. Now, I want to utilize this in my ASP.NET in Global.asax file. However, I am feeling a bit ...

The label and its .input-group in Bootstrap 5 are not aligned on the same line

<div class="input-group mb-3"> <label class="input-group-text form-floating ">Please Share Your Name *</label> <input type="text" class="form-control" placeholder="First Name&quo ...

Implementing double dependent dropdown functionality using Ajax in CodeIgniter

My issue involves two dropdowns where the result is supposed to appear in a text input. However, when the second dropdown has multiple selections, the result always reflects the last selection made. For example, if the first dropdown has options A and B, t ...

Is it possible to modify the background-color of a minified gallery using CSS in jQuery?

As I was brainstorming ideas for my website, I thought about the possibility of creating a small preview gallery for background images. The goal would be to allow users to hover over an image in the gallery and have the CSS background image of the site cha ...

Transfer information to the form through a hyperlink

I need help with a script that sends data to a form when a link is clicked. What I'm trying to achieve is to have the data appear in the form when the user clicks the link below, which is generated from a database. <div id="menu_bar" region="west ...

ng-table Filtering with dropdown selection

Hello, I am currently working on creating a Ng-table with a select dropdown menu for filtering the results. Here is where I am at so far. 1) How can I remove one of the pagination options that appear after applying the filter? I only want to keep one pagi ...

Can we generate a JSON format that resembles the following structure?

Currently, I am in the process of transferring data from one system to another. The developer at the remote system has provided me with an example of a JSON structure that should be included in the body of the REST call. The structure is outlined below: ...

Having trouble with the clear button for text input in Javascript when using Bootstrap and adding custom CSS. Any suggestions on how to fix

My code was working perfectly until I decided to add some CSS to it. You can view the code snippet by clicking on this link (I couldn't include it here due to issues with the code editor): View Gist code snippet here The code is based on Bootstrap. ...