How to apply an active class to the current hyperlink in ASP.NET using C#

I am attempting to highlight the current active link using jQuery in an ASPX web form. First, I retrieved all hyperlinks from my database to gallery.aspx using the following function in the code-behind:

public string build_Gallery_Category()
{
    string post = "";
    DataTable dt = new DataTable();
    clsBusiness cls = new clsBusiness();
    dt = cls.Fill_In_DataTable("GalleryGroup");
    if (dt.Rows.Count > 0)
    {
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            post += "<li><a href='gallery.aspx?gid=" + dt.Rows[i]["GalleryId"].ToString() + "'><strong>" + dt.Rows[i]["GalleryName"] + "</strong></a></li>";
        }
    }
    return post;
}

After calling this method, all hyperlinks are displayed on gallery.aspx as shown below:

<div id="menu">
    <ul>
       <%=viewCategory %>
    </ul>
</div>

Next, add this style to gallery.aspx:

<style>
    .active { background: #f00; }
</style>

Finally, I added the following jQuery code at the end of the form:

<script>
    $("menu li").click(function (e) {
        e.preventDefault();
        $("menu li a.active").removeClass("active"); //Remove any "active" class  
        $("a", this).addClass("active"); //Add "active" class to selected tab  
    });
</script>

However, I am encountering issues with my code. Any assistance would be greatly appreciated. Thank you.

Answer №1

It appears that you have an error in your jQuery selector

$("menu li")

should actually be

$("#menu li")

Similarly,

$("menu li a.active")

should be changed to

$("#menu li a.active")

If you omit the #, you will not be able to target the div element with the ID of menu

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

Styling: How can I create indentation for a specific text area while leaving another unaffected?

I am attempting to format this complete form with an indentation: <%= form_for([micropost, micropost.comments.build], :html => { :id => "blah_form" }) do |f| %> <div class="field"> <p2>Who are you?</p2> ...

Guide on how to efficiently upload serialized image data using jquery and PHP technology

I am attempting to crop and upload images using the extension called http://scottcheng.github.io/cropit/ within the context of OpenCart 3. However, I am unsure of how to upload serialized image data. This is the HTML section of the code which represents t ...

Can you guide me on how to replicate this for an additional 16 times? Apologies for the confusion

I need help with a code that can apply to multiple names at once, rather than one by one each time. Here's the example HTML code: <div id="one" class="text-container"> <span> Campfire Cooking in Another World wi ...

ways to set a background color for a textarea element using bootstrap

How can I add a background color to my text area field in Bootstrap? <div class="form-group"> <div class="col-xs-12"> <textarea class="form-control" id="exampleTextarea" rows="6" placeholder="Message" style="background-color: #f ...

Issue with JQuery dialog not triggering autocomplete

I have integrated the JQuery 1.7.2 and JQuery-UI 1.8.18 libraries with both http://docs.jquery.com/UI/Dialog and http://docs.jquery.com/UI/Autocomplete. On a standard page load, the autocomplete function works perfectly with a text box in a form. It' ...

Encountering difficulty accessing website due to HTTP Error 500.19 - Internal Server Error

My website is currently being hosted on localhost using IIS 10.0, yet it is encountering an error during the loading process. https://i.sstatic.net/RhU4V.png ...

What is the correct way to utilize indexOf() in a case-insensitive manner?

I need help with a list of strings: List<string> fnColArr = new List<string>(); fnColArr={"Punctuation,period,Space,and,yes"}; Currently, I am using the IndexOf method on the List to search for a specific string in the list: int arrayval = f ...

Customize GUI - adjusting menu dimensions and placement (javascript / css)

I’ve been experimenting with integrating Dat GUI into my Three.js project for adding some controls. The interface of Dat GUI really appeals to me, but I’m facing challenges in terms of positioning the menu on the page. Specifically, I’m looking to ce ...

`How to extract data-preconnect-urls using HTMLAgilityPack`

Currently, I am utilizing HTMLAgilityPack in an attempt to extract the link located within the data-preconnect-urls. Could you provide guidance on how I can achieve this? <h3> <a style="display:none" href="/aclk?sa=L&amp;ai=DChcSEwimnPnc ...

Exploring JSON Data and Presenting it on Screen

I'm currently working on deserializing some JSON data. { "topalbums":{ "album":[ { "name":"Slumdog Millionaire", "playcount":1442872, "url":"", "artist":{ "name":"A.R. Rahman", "mbid":"e0bba708-bdd3-478d-84ea-c706413bedab", "url ...

Tips for concealing the ID value within a URL or parameter

I just started learning Angular JS and I have a question about hiding parameters in the URL when clicking on anchor tags to send data to another controller. I don't want any ID or its value to be visible in the URL. Is it possible to hide parameters i ...

Image malfunction in jquery blockui occurs following multiple AJAX requests, except when within the success function of an AJAX request

I have a perplexing question that has been on my mind. While I am aware of the issue and its resolution, I am struggling to comprehend the underlying reason. There is a particular method that goes through some preliminary steps, validation, saving, and cl ...

Explore websites and interact with clickable elements on your iPad or iPhone

I've been working on creating a responsive website for tablets like iPhone and iPad, but I'm running into an issue with clickable elements. Specifically, my navigation is currently set to "position:fixed" so that it stays visible as you scroll d ...

Apply a CSS class to the <tr> element when it is in an even

I'm currently implementing a dynamic row addition feature in a table using the code snippet below. My next task is to determine whether the newly added row is odd or even, and add a specific CSS class to the <tr> element if it's even. Doe ...

Exiting feature of logging out

Is there a way to log out a user on my webpage and redirect them to the login page while also preventing them from viewing the previous page? I've been using FormsAuthentication.SignOut(); but I'm not sure if it's sufficient for logging ou ...

Learn how to update a form dynamically using the Ajax.BeginForm feature

I am currently working on updating a form within my application using Ajax.BeginForm. The update process is functioning correctly, however, upon executing the function in the controller, it redirects to a view with the controller function name displayed. ...

Incorporate a polygon or circle shape seamlessly into the surrounding Google Map environment

Currently, I have a circle created using maps.Polygon in my code. By setting the strokeWeight to 0, I can remove the outline of the polygon. Is there a way to blend the circle seamlessly into the map so that the transition between the circle and the rest o ...

Creating a Blazor Server Application: Tips for Ensuring Consistent Data Retrieval for Client Requests

Imagine we have a basic classical timer set up in the default Blazor Server. <p>Current count: @currentCount</p> <button class="btn btn-primary" @onclick="IncrementCount">Click me</button> @code { private i ...

Customizing Bootstrap Navigation: Creating Space Between Menu Items

I've configured my Bootstrap navigation with a dropdown toggle for "Coverage". I'm looking to decrease the spacing between each list item (li) under this dropdown. What CSS setting should I use to achieve this? Interestingly, when I apply float: ...

Using Three.js to showcase a <div> positioned above a canvas featuring a dynamic skybox

The process I used to create a skybox on a Three.js canvas is as follows: var urls = [ 'pos-x.jpg', 'neg-x.jpg', 'pos-y.jpg', 'neg-y.jpg', 'pos-z.jpg', 'neg-z.jpg' ] window.cubemap = ...