3

So what I want to do is determine if the current link is 'active', i.e. if this link was just clicked by the user.

If it is, then I want to apply the class=selected to that <li> item.

So this is what I am working with:

<ul>
   <li><a href="#">Link 1</a></li>
   <li><a href="#">Link 2</a></li>
   <li><a href="#">Link 3</a></li>
   <li><a href="#">Link 4</a></li>
   <li><a href="#">Link 5</a></li>
</ul>  

If a link is selected, I want the <li> to look like this:

<li class="selected"><a href="#">Link 1</a></li>

So the only differences is that one has a class applied to it, and the other doesn't.

I want to do this in an erb file. So Ruby would be the language of choice...although, it's not Rails (Sinatra to be exact).

Thanks.

Edit 1

Using jQuery might be necessary and that will be fine.

Phrogz
  • 296,393
  • 112
  • 651
  • 745
marcamillion
  • 32,933
  • 55
  • 189
  • 380
  • the :active pseudoclass is set _while the user is clicking on the link_. @marcamillion, is this what you want, or are you wanting to change the styling of the link after the user has arrived at the page it points to? – bonkydog Nov 07 '11 at 23:39
  • @bonkydog yes, that is what I am looking for. Something like that. But it has to work on all major browsers, obviously. Is the `:active` pseudoclass widely supported? Think of it as I am creating some tabbed navigation that will be moving around based on the interactions with the user. As a user clicks on one link, the appearance should change according to the new class applied to it. – marcamillion Nov 07 '11 at 23:45
  • a:active is supported since IE4 (that's where it started), and should be in most/all Firefox/Chrome versions. Some browsers may respond to li:active, but probably not older ones. Chrome accepted both `li:active { background-color: black; }` and `li > a:active { background-color: pink; }` FWIW. – JasonTrue Nov 08 '11 at 00:13
  • @JasonTrue care to write out a full answer including a code example? – marcamillion Nov 08 '11 at 00:17
  • (IE 7 didn't, but IE8 or 9 might handle those selectors) – JasonTrue Nov 08 '11 at 00:18
  • Fair enough, since I've done more research now :P still don't have an IE-suitable solution that meets the exact request, though. – JasonTrue Nov 08 '11 at 00:29
  • Am I right in my assumption that the `href`s will be fragments and not result in a trip back to the server if the user clicks on them? – Ian Eccles Nov 08 '11 at 14:37
  • @Ian yes, you are right in that assumption. Basically it's dynamic Ruby powered navigation that I am building. – marcamillion Nov 08 '11 at 20:23
  • 1
    Are you going to be firing a javascript event (or similar action) whenever a link is clicked in order to change content on the page? If so, within the click event function, just remove the class "selected" from all links in that `ul` (give the `ul` an ID to make the javascript DOM search quicker) and then add the class "selected" just to the clicked link (ex/ using `$(this).addClass('selected')` within the click event function with jQuery). – William Nov 08 '11 at 21:50
  • @William looks like that's exactly what I am going to have to do. Thanks. – marcamillion Nov 08 '11 at 23:34