I’m trying to insert HTML strings before and after an HTML string before writing the finished string to the DOM. Here’s what I have so far:
var Hello = '<p id="1234">Hello</p>';
var $Hello = $(Hello);
var $HelloGoodbye = $Hello.after('<p>Goodbye</p>');
$('#content').append($HelloGoodbye);
The result is that my #content div only gets the Hello fragment appended.
Thanks in advance!
The problem is because
$Hellois a reference to the<p>element. You are putting the ‘Goodbye’<p>outside of the first, so the$Hellovariable remains unaffected. You need some method of grouping the two together if you want to have them in a single variable. Try this:Example fiddle