views:

117

answers:

3

I have a link:

<a id="theLink" href="h***://stackoverflow.com">Go to SO</a>

How can I use prototype to either strip <a>'s or just leave the innerHTML so that the elements becomes:

[h***://stackoverflow.com] (something that is no longer clickable)?

Or maybe convert the <a> to a <span>

A: 

onclick="return false;"

asdf
+1  A: 

try this

function removeAnchor() {
        var link = document.getElementById('theLink');
        var span = document.createElement("span");
        var txt = link.href;
        var textNode= document.createTextNode(txt);
        span.appendChild(textNode);
        link.parentNode.replaceChild(span, link);
}
TeKapa
A: 

To replace the <a> with a <span> with the initial element's href as content:

$('theLink').replace((new Element('span')).update($('theLink').href));

The result will be:

<span>h***://stackoverflow.com</span>

To replace the with a with the same content:

$('theLink').replace((new Element('span')).update($('theLink').innerHTML));

Which will result:

<span>Go to SO</span>
Victor Stanciu
i think he want something pure javascript (without jquery)
TeKapa
He said "How can I use prototype...". He means the Prototype JavaScript framework, and that's what my example uses.
Victor Stanciu