replace link with its href using jquery – Education Career Blog

i have a p tag that has many anchor tags and some text. i want to replace each of the anchor tags with its respected href.


I interpret your question that you want to replace the whole tag, not only the contained text, like so:

You are looking for .replaceWith():

$('p a').replaceWith(function() {
    return $(this).attr('href');


Try this:

$('p').find('a').each(function ()
    var $this = $(this);



CSS3 solution without using jQuery:

<style type="text/css">
    a > span { display: none; }
    a:after { content: attr(href); }
<a href=""><span>Test</span></a>

gracefully degrades for non-CSS3 browsers.


Pretty similar, I know…


Leave a Comment