Forum Moderators: open

Message Too Old, No Replies

Dynamically creating links from span tags in Javascript

         

thing3b

12:54 am on Jan 30, 2007 (gmt 0)

10+ Year Member



What I am attempting to do is to convert all <span class="clickable"> tags into clickable links. It is odd that with all the SEO sites around, that nothing like this has been mentioned.

The original code I have is:


<span class="clickable">Link Text</span>

I am trying to get client-side javascript to convert this on the fly to:

<span class="clickable"><a href="/archive/link+text">Link Text</a></span>

With the url generated from the link text and put the javascript equivilent of the PHP functions "StrToLower" and "UrlEncode".

Any ideas on how to do this or pointers in the right direction will be worshipped...

Little_G

1:27 am on Jan 30, 2007 (gmt 0)

10+ Year Member



Hi,

try:

[pre]
<script type="text/javascript">
window.onload=function(){
var spans = document.getElementsByTagName('span');
for(var i=0;i<spans.length;i++){
if(spans[i][i].className == "clickable"){
spans[i].innerHTML = '<a href="/archive/' + escape(spans[i].innerHTML.toLowerCase()) + '">' + spans[i].innerHTML + "</a>";
}
}
}
</script>[/pre]

[/i]Andrew