I have a varying number of elements with the same class name:
<p class="test"></p>
<p class="test"></p>
I want to replace each of these with a new set of elements with the same class name, but there could be more or fewer elements:
<p class="test"></p>
or
<p class="test"></p>
<p class="test"></p>
<p class="test"></p>
If I use $('p.test').replaceWith('new content'), it will replace each element with the content so I can’t do that.
What is the best way to accomplish this?
EDIT (another explanation):
So I have these elements on the page:
<p class="test"></p>
<p class="test"></p>
<p class="test"></p>
Then, I dynamically retrieve a new set of elements (a varying number of them) that have the same class name (and maybe some other class names). For example:
<p class="test classa"></p>
<p class="test classb"></p>
In the above example, I want the first three <p> elements to be gone. And in their place should be the 2 dynamically retreived <p> elements.
Keep in mind the number of elements of either set can vary.
Updated:
assuming you have your
<p>'sinto a containerYou could delete them, and append (or prepend) the new elements
I assume your new set of elements are into the data array