Monday, June 21, 2010

JQuery automatic linking

I have some values on my webpage, they are URLs and I have wrapped them in the form:


<span class="url">google.com</span>

<span class="url">http://lwn.net</span>

What I would like is to reneder anything that is of class 'url' as a link. And I would also like to stick a http:// infront of links that are currently missing them.

For this I use JQuery (I should get around to doing something sensible in the database as well but not just now).

So the JQuery looks like:
  $(".url:not(:contains('http://'))").prepend("http://");
  $(".url").each(function() {
    $(this) .replaceWith("" + $(this).text() + "");
   });

That's it.

No comments: