views:

576

answers:

4

I want to link to bookmark on a page (mysite.com/mypage.htm#bookmark) AND visually highlight the item that was bookmarked (maybe having a red border). Naturally, there would be multiple items bookmarked. So that if someone clicked on #bookmark2 then that other area would be highlighted).

I can see how to do that with .asp or .aspx but I'd like to do it more simply than that. I thought maybe there was a clever way to do it with CSS.

WHY I'm interested: - I want to have our programs link to a shopping page that lists all the programs on it. I'm using a bookmark so they're jumping to the particular program area (site.com/shoppingpage#Programx) but just to make it obvious I'd like to actually highlight the page being linked to.

A: 

I guess if you could store this information with Javascrit and cookies for the functionality of remembering the bookmarks and even add a splash of Ajax if you wanted to interact with a database.

CSS would only be able todo the styling bit. You would have to give the bookmarked anchor a class found in ur css file.

CSS also have the a:visited selector which is used for styling links found in the browsers history.

GateKiller
+1  A: 

Use your favorite JS toolkit to add a "highlight" (or whatever) class to the item containing (or contained in) the anchor.

Something like:

jQuery(location.hash).addClass('highlight');

Of course, you'd need to call that onready or click if you want it triggered by other links on the page, and you'll want to have the .highlight class defined. You could also make your jQuery selector traverse up or down depending on the container you want highlighted.

Kevin
This doesn't work, as it tries to find an element with the ID of the hash, not an anchor with the NAME attribute.
samjudson
samjudson, you shouldn't have anchors with a name attribute. Hash anchors should point at id attributes in modern HTML.
Ryan McGeary
+5  A: 

In your css you need to define

a.highlight {border:1px solid red;}

or something similar

Then using jQuery,

$(document).ready ( function () { //Work as soon as the DOM is ready for parsing
 var id  = location.hash.substr(1); //Get the word after the hash from the url
 if (id) $('#'+id).addClass('highlight'); // add class highlight to element whose id is the word after the hash
});

To highlight the targets on mouse over also add:

$("a[href^='#']")
 .mouseover(function() {
  var id  = $(this).attr('href').substr(1);
  $('#'+id).addClass('highlight');
 })
 .mouseout(function() {
  var id  = $(this).attr('href').substr(1);
  $('#'+id).removeClass('highlight');
 });
Pat
This is great - you also might want to select the parent element of the anchor, rather than the anchor itself however.
samjudson
In modern HTML, hashes should point at id attributes, not name attributes.
Ryan McGeary
Thanks for the tip Ryan, fixed the code to work on ids instead of name attributes, so it is a bit cleaner now.
Pat
+3  A: 

You can also use the target pseudo-class in CSS:

<html>
<head>

<style type="text/css">
div#test:target {
 background-color: yellow;
}
</style>

</head>
<body>

<p><b><a href="#test">Link</a></b></p>

<div id="test">
Target
</div>

</body>
</html>

Unfortunately the target pseudo class isn't supported by IE or Opera, so if you're looking for a universal solution here this might not be sufficient.

chrisofspades