0

In the following string, I'm trying to add an additional span around the post.caption but keep it inside the <span class="overlay" />. I've tried prepend and wrap before and after the .html, but can't get it or anything to do it correctly.

$('<span class="overlay" />').html(post.caption).appendTo(entry);

Result would look like <span class="overlay"><span>Caption content</span></span>

Here's the full snippet if that helps:

entry = $('<a />').attr('href', post.permalinkFull)
        $('<img />').attr('src', post.image).appendTo(entry)
        $('<span class="overlay" />').html(post.caption).appendTo(entry);

Any help would be hugely appreciated!

5
  • 1
    What is the value of post.caption? Commented Mar 14, 2013 at 18:11
  • Shouldn't be <span class="overlay"></span> ? Commented Mar 14, 2013 at 18:12
  • @EmCo Not necessarily. $('<span class="overlay" />') works fine as well Commented Mar 14, 2013 at 18:12
  • 1
    @Ryan, when are you adding entry to the document? Commented Mar 14, 2013 at 18:15
  • 1
    Seems to work just fine for me -> FIDDLE ??? Commented Mar 14, 2013 at 18:17

2 Answers 2

3

if post.caption is a string, then as I understand it you also want to create the span around it:

$('<span>').html(post.caption).appendTo(entry).wrap('<span class="overlay" />');
Sign up to request clarification or add additional context in comments.

3 Comments

This one worked! The ordering of everything. Many thanks @mitai and all that chimed in.
Good luck! Please remember to mark the answer as correct, I need a few more points before I can comment on stuff around here :)
great, I just got the "comment everywhere" privilege, I'm on my way to the top :P
3

You can try this with wrapInner:

$('<span class="overlay" />').html(post.caption)
                             .wrapInner('<span />').appendTo(entry);

Comments

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.