tags:

views:

148

answers:

2

I want to disable all links within a div and replace it with the content that was there.

i.e.

<div><a href="/blah.html">My super link</a></div>

to

<div>My super link</div>

Thanks

+2  A: 

You can do it using .replacewith() and a function, like this:

​$("div a").replaceWith(​​​​function() { return $(this).text();​ });​

You can see a quick demo here

Nick Craver
Will this work for images and other content?
Chris
@Chris - Use `.contents()` instead of `.text()` in that case, see a demo here: http://jsfiddle.net/qd3E2/2/
Nick Craver
+2  A: 

Hmm - mine is similar to the other quicker-typists' answers, but I would have thought html() would have been what you wanted to use:

$('div a').each(function() {
    $(this).replaceWith($(this).html());
});
nickf
+1 - While ours work for the example, this would work in all applicable cases.
Nick Craver
I have some ` `'s inside my `<a>` and this seems to throw an exception : "Syntax error, unrecognized expression:      "
Chris