You can iterate elements compare its Node.nodeType, If node is TEXT_NODE(3)
. Then use .replaceWith() to replace the text node with anchor element
$('#ProductCode').contents().each(function () {
if (this.nodeType == 3) {
var elem = $('<a>', {
href: "http://www.example.com/" + this.nodeValue,
text: this.nodeValue
});
$(this).replaceWith(elem);
}
});
EDIT
.wrap() is better option
$('#ProductCode').contents().each(function () {
if (this.nodeType == 3) {
var elem = $('<a>', {
href: "http://www.example.com/" + this.nodeValue
});
$(this).wrap(elem);
}
});