Anderson Green
Anderson Green

Reputation: 31840

Convert hyperlinks to plain text using JavaScript

Using JavaScript, is it possible to convert a hyperlink to plain text (so that it no longer links to any page?)

For example, would it be possible to change the following link (inside the <a> tag) to plain text using JavaScript (so that this text no longer links to any page)?

<html>
    <body>
        <a href="http://www.wikipedia.org/" target="blank" id = "wikiLink">Go to Wikipedia</a>
    </body>
</html>

Upvotes: 0

Views: 5434

Answers (5)

laurent
laurent

Reputation: 90804

With jQuery a simple way is to remove the link and to put a text node instead:

var $link = $('#wikiLink');
$link.after($link.text());
$link.remove();

Upvotes: 0

Vasanth Umapathy
Vasanth Umapathy

Reputation: 1741

Following code should render hyper link as text.

$(element).find('a').contents().unwrap();

JS BIN Demo

Upvotes: 3

techfoobar
techfoobar

Reputation: 66693

If you want to remove the "link" behavior of all links, you can do:

var links = document.getElementsByTagName('a');
for(var i=0; i<links.length; i++) links[i].href = '';

Upvotes: 0

Adam Coulombe
Adam Coulombe

Reputation: 1505

document.getElementById("wikiLink").removeAttribute('href')

or with jquery

$("#wikiLink").removeAttr('href');

Upvotes: 3

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

The simplest way would be to remove its href attribute:

document.getElementById('wikiLink').removeAttribute("href");

If you want to completely remove the node, leaving the text, try this:

var toRemove = document.getElementById('wikiLink'), parent = toRemove.parentNode,
    text = toRemove.firstChild;
parent.insertBefore(text,toRemove);
parent.removeChild(toRemove);
parent.normalize();

Upvotes: 3

Related Questions