2

I have an each loop in which each looped element has the following html structure:

<li>
    <p>Text</p>
    <form>...</form>
</li>

In the loop I create a <span> element, which I want to insert in each looped element between the <p> and the <form> element. But I don't know how to access the elements within this, I just know how to insert it at the beginning (prepend) and end ('append') of the li, but not between the <p> and <form>.

Here is my code:

function calculation(elements) {
    $(elements).each(function() {
        ...
        var output = $('<span>').addClass('durationValue').html('Some Values..');
        $(this).append(output);
    });
}
2
  • Can you create a fiddle? Commented May 15, 2014 at 20:17
  • 1
    You could use before() or after() to perform the append. api.jquery.com/before api.jquery.com/after Commented May 15, 2014 at 20:17

1 Answer 1

2

You can use .after() or .before() instead of .append() or .prepend() like so:

$(this).find("form").before(output);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I didn't know that I can use something like find() in the this context. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.