If I understood, you're trying to wrap the whole article
item in a link which href
is the one on the hyperlink within the h3
, and replace this link with common text (span
).
Try this way:
$(function () {
$("div.article-image").each(function () {
var link = $(this).next("h3").find("a");
var linkHref = $(link).attr("href");
var linkText = $(link).html();
var wrapper = $("<a />", {
href: linkHref
});
var linkAsText = $("<span />").html(linkText);
$(this).parent().append($(wrapper));
$(wrapper).append($(this).parent().children());
$(link).replaceWith($(linkAsText));
});
});
Demo: http://jsfiddle.net/JpArk/2/