Hannoun Yassir
Hannoun Yassir

Reputation: 21182

replace link with its href using jquery

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.

Upvotes: 0

Views: 282

Answers (4)

teampoop
teampoop

Reputation: 11

Pretty similar, I know...

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

Upvotes: 0

jrn.ak
jrn.ak

Reputation: 36619

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.

Upvotes: 1

Felix Kling
Felix Kling

Reputation: 816262

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');
});

Upvotes: 7

Matt Ball
Matt Ball

Reputation: 359776

Try this:

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

Demo

Upvotes: 1

Related Questions