36
<a href="http://www.website.com/something" title="Show Profile">Mentalist</a>

Whenever a hyperlink has a title of "Show Profile" I want to remove the hyperlink and replace it with only with the text.

So instead of

<a href="http://www.website.com/something" title="Show Profile">Mentalist</a>

I want to have only Mentalist.

Any idea how to solve that?

Lightness Races in Orbit
  • 369,052
  • 73
  • 620
  • 1,021
matt
  • 40,185
  • 99
  • 257
  • 397

4 Answers4

95

this should work:

$('a[title="Show Profile"]').contents().unwrap();

Here a Fiddle with the proof.

DanielB
  • 19,386
  • 2
  • 42
  • 49
3

This will do:

<a href="http://www.website.com/something" title="Show Profile">Mentalist</a>
<a href="http://www.website.com/something" title="Something Else">Mentalist</a>

<script type="text/javascript">
$("a[title='Show Profile']").each(function(){
    $(this).replaceWith($(this).text());
});
</script>

It should replace only the first link.

rciq
  • 1,291
  • 10
  • 10
  • DanielB has provided a much nicer solution here: http://stackoverflow.com/questions/6188277/remove-hyperlink-but-keep-text/6188344#6188344 – rciq May 31 '11 at 13:38
2

To do this on links of multiple classes,

$("a.className1, a.className2").contents().unwrap();
Prem
  • 5,474
  • 3
  • 23
  • 23
2

Vanilla JavaScript way (instead of jQuery) to remove hyperlink but keep text:

const links = document.querySelectorAll('a[title="Show Profile"]')

links.forEach(link => {
    const el = document.createElement('span')
    el.textContent = link.textContent
    link.parentNode.replaceChild(el, link)
})
Yuci
  • 22,858
  • 8
  • 99
  • 108