Please consider the below HTML and Javascript. In the script, I am replacing an a tag with a p tag. I am expecting the alert() function to return the contents of the p tag but instead it returns the contents of the original a tag which no longer exists.
How can I reference the new element?
HTML:
<a href="">This is a link</a>
Javascript:
$(document).ready(function() {
$("a").each(function() {
$(this).replaceWith('<p>New Paragraph</p>');
alert($(this).text());
});
});
.replaceWith()
returns the previous object, the<a>
, not the<p>
it replaced it with. – Kazimir