Hi knal

This works for me:

$(function(){
 $('#my_list a').each(function(){
   var href = $(this).attr("href");
   /* remove trailing slash if present */
   if (href[(href.length-1)]==="/") {
     href = href.substr(0,(href.length-1));
   }
   /* now get the last part of the path */
   var anchorname = "";
   if (href.lastIndexOf("/")===-1) {
     /* no more slashes - return full path */
     anchorname = href;
   } else {
     /* get everything after the last slash */
     anchorname = href.substr((href.lastIndexOf("/")+1));
   }
   /* now set the href to the anchor */
   $(this).attr("href", "#"+anchorname);
 });
});

A regex would be more elegant if I had the time to write one and test it, but this should suffice for most situations.

Peter

on 17/08/2009 20:07 knal said::
Hi group,

I'm looking for a correct way of manipulating <a hrefs...
The code looks like this:

<ul id="my_list">
  <li><a href="/portfolio/category/animals/">Animals</a></li>
  <li><a href="/portfolio/category/buildings/">Buildings</a></li>
  <li><a href="/portfolio/category/cars/">Cars</a></li>
  <li><a href="/portfolio/category/people/">People</a></li>
</ul>

and with jQuery i want to manipulate it into this:

<ul id="my_list">
  <li><a href="#animals">Animals</a></li>
  <li><a href="#buildings">Buildings</a></li>
  <li><a href="#cars">Cars</a></li>
  <li><a href="#people">People</a></li>
</ul>

So only the last element in the URL has to stay, and a hash has te be
added...
I wouldn't know how to achieve this. I've been googling it for three
hours now, but i jus't can't get it to work.
Any help would be greatly appreciated!

Thanks,
Knal

Reply via email to