Emphasize the hyperlinked title

Is there a way to automatically add a CSS class "highlight" to headlines or sections based on anchors such as "...this.html#headline1" (similar to the table of contents on Wikipedia)?

I am looking for a solution that will also work when navigating from a different page with a link like this: , and not just through onclick events like shown in this example: Highlight a # section in a page - jQuery

Answer №2

Detect the presence of window.location.hash and emphasize the corresponding id, as long as your webpage follows that structure.

Styling with CSS

.highlight{ background-color :#FCFC9F; }

Implementing with jQuery

function applyHighlight() {
  var hash = window.location.hash;

  if(hash) {
    $(hash).addClass('highlight');
  }
}

applyHighlight(); // Execute this function when the page loads

$('a').on('click', applyHighlight); // Perform this action on every `a` click

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

Tips for invoking a particular function when a row in a Kendo grid is clicked

I have a Kendo grid named "mysubmissionsGrid" and I want to execute a function when each row is clicked with a single or double click. How can I accomplish this? <div class="col-xs-12 col-nopadding-left col-nopadding-right" style="padding ...

Connects URLs to the displayed outcomes in jQuery Auto-suggest Interface

This particular code snippet has been modified from a tutorial on jQuery autocomplete <!doctype html> <html lang="en> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> ...

Retrieving the value of a radio button using JavaScript

I am working with dynamically generated radio buttons that have been given ids. However, when I attempt to retrieve the value, I encounter an issue. function updateAO(id2) { var status = $('input[name=id2]:checked').val(); alert(status); ...

CSS media queries to exclude a property for mobile devices

I have a CSS class with the width property set to calc(100% - 17px). However, I want to ignore this property completely for devices with specific resolutions. I've tried using media queries and values like initial, inherit, and unset for the width pro ...

optimal application of css with jquery

I have a question about using jQuery to set the padding of a class or id. I am able to successfully change the height of an element with this code: $('.menu').css({ height: '90px' }); But now, I need to apply a specific CSS rule in jQ ...

Determine the text's length inside a div element by utilizing the div itself

I have a div with a fixed width of 30% and I am trying to find the length of the text within that div. Check out my JSFiddle for the code. The current code snippet I am using is: var text=$('#test').text(); text=text.trim(); var len=text.lengt ...

How do I troubleshoot and resolve this problem with jQuery and Ajax?

I have 4 text inputs that look like this: <input type="text" name="amount" id="amount"> <input type="text" name="commission" id="commission"> <input type="text" name="discount" id="discount"> <input type="text" name="total_amount" id ...

Should you choose a pre-built CSS framework or create your own from scratch?

Have you come across a framework that meets your project needs with just a few tweaks, or have you forged your own path along the way? What advice do you have for someone facing this decision and focusing on priorities like: Implementing a CSS reset Refi ...

I am seeing an empty dynamic HTML table

I have been working on creating a dynamic HTML table, but I am facing an issue where the data is not displaying in the table. I have verified that the query is correct by testing it in SQL and confirming that it outputs the data. The problem seems to lie w ...

Fetching JSON array from servlet with AJAX

I have two JSON arrays in my servlet. I am trying to retrieve the values from the JSON arrays and display them on a JSP page using AJAX. Here is the code snippet: JSONArray htags = new JSONArray(); htags.add("#abc"); htags.add("#xyz"); ...

Personalized FileInput within a card - Bootstrap 5

I am attempting to incorporate a custom file selection feature within a Bootstrap card: Here is my attempt: <style> label { cursor: pointer; } .custom-input-image { opacity: 0; position: absolute; z-index: -1; } </style> <div ...

Adjusting the size of text with Bootstrap 3's font size buttons

I recently created a website using Bootstrap 3 and now I am looking to add two buttons. One button will increase the font size of all elements on the page, while the other button will decrease it (specifically for users with disabilities). For this functi ...

The <entry> rises above the backdrop of my introduction

Having trouble with my website development... I want to insert an <article> after the <header> with a full-screen background intro, but it seems to overlap with my <div> even though it's in the header and the article is in the body. ...

The footer fails to adhere to the bottom of the page

Struggling to keep my footer at the bottom of the page when there is minimal content. I attempted using: position:absolute; and bottom:0; It appears to stay in place, but when more content is added, it ends up going "under" the footer that remains n ...

"Developing CSS styles that work seamlessly across all

What methods do professional web designers use to develop cross-browser compatible CSS? Is it typically a manual process, or are there specific toolkits available to simplify the task similar to YUI for JavaScript? I am looking to avoid WYSIWYG editors s ...

Simple steps to generate a static header in list view on asp.net

I'm struggling to keep my header fixed while scrolling and can't seem to figure it out. Below is my ListView Code: <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="FormSectionSubSectionItemRelID" OnSele ...

Adjusting the Size of Dialog Windows in Lightswitch HTML Client

When using the MS Lightswitch HTML Client, if a dialog menu option contains too many characters to fit within the length of the dialog window, the text will be cut off and replaced with (...). Is there a way to add a scroll bar or adjust the text size in ...

Adjustable picture sizes

There's an issue I'm experiencing where images take a moment to load when the page loads. This not only affects the appearance but also interferes with the functionality of a scroll-up button that needs to be positioned at the bottom of the page ...

Learn how to incorporate a vertical divider pipe into your website using Bootstrap

Hello, I'm having trouble adding a separator between nav-links that actually works. I tried adding the following code snippet: nav-link:after { content:"|"; } But unfortunately, it's either not working at all or appearing on the left side of th ...

Executing a PHP function with an onclick event on an `<li>` tag via AJAX: What's the best approach?

Can you assist me in using AJAX to call a PHP function that communicates with an external server when the onclick event is triggered? <div class="container"> <h3 style=color:blue;>DETAILS </h3> <ul class="nav nav-pills" style="backgro ...