Jamil
Jamil

Reputation: 1

How to change a link into a span of text or some kind of non-anchored element with prototype

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>

Upvotes: 0

Views: 2516

Answers (2)

TeKapa
TeKapa

Reputation: 546

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);
}

Upvotes: 2

Vic
Vic

Reputation: 12527

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>

Upvotes: 0

Related Questions