Reputation: 21182
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
Reputation: 11
Pretty similar, I know...
$("a").each(function(){
$(this).text($(this).attr('href'));
});
Upvotes: 0
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
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