37
<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
  • 378,754
  • 76
  • 643
  • 1,055
matt
  • 42,713
  • 103
  • 264
  • 397

4 Answers4

96

this should work:

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

Here a Fiddle with the proof.

DanielB
  • 19,910
  • 2
  • 44
  • 50
4

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
  • 27,235
  • 10
  • 114
  • 113
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,309
  • 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,844
  • 3
  • 25
  • 23