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: http://jsfiddle.net/xXmWj/1/

You are looking for .replaceWith():

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

,

Try this:

$('p').find('a').each(function ()
{
    var $this = $(this);
    $this.html($this.attr('href'));
});

Demo

,

CSS3 solution without using jQuery:

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

gracefully degrades for non-CSS3 browsers.

,

Pretty similar, I know…

$("a").each(function(){
  $(this).text($(this).attr('href'));
});

Leave a Comment