If I have html like so:
<div>
<span></span>
<span></span>
<span></span>
</div>
How would I insert some text before before the last span?
Like this:
<div>
<span></span>
<span></span>
new text
<span></span>
I have $("div span:last").prev().append("new text");
which puts the new text inside a span tag.
Note: The number of span tags inside of the div tag can change dynamically.
.prepend()and.append()are functions used for inside insertion. You are probably looking for either.before()or.after(), which are functions for outside insertions that will insert into the DOM at the same DOM level of the found selector.