1

I have a bit of code that concatenates text from some input boxes and the resulting string is then put into a <span>. I've got a '+' button which copies the <span> and appends it to the end of a <div>.

Now I want to add a <br> at the end of the <span> so that each span is placed on a new line but when I do this pressing the "+" returns [object][Object] as opposed to the values! I've setup a js fiddle here.

You will see what I mean when you click '+'. It's probably something very simple but I can't for the life of me find out what is causing this!

Help is much appreciated.

2 Answers 2

3

Because using the + operator with a string coerces everything to string, resulting in the string representation of the jQuery object (which is usually [Object object]). Try:

$("#five").append(creativeCopy.html() + "<br>");

or:

$("#five").append(creativeCopy).append("<br />");
Sign up to request clarification or add additional context in comments.

Comments

0

I have corrected the code

$(document).ready(function() {

    var creativeUniqueId = 1;
    $(".addCreative").click(function() {
        var creativeCopy = $("#creative_text").clone();
        var creativeCopyId = "creative_text" +creativeUniqueId;
        creativeCopy.attr("id",creativeCopyId);
        $("#five").append(creativeCopy);
         $("#five").append("<br>");
        creativeUniqueId++;
    }); 
});

You can see at http://jsfiddle.net/aexK8/3/

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.