0

I have a variable which contains text and a tag. When appending variable to the element the tag isnt interpreted as a tag. Instead its interpreted as text.

Take this example:

var translationWrapper = $("<div class='list-label'></div>");
translationWrapper.html('<span> test </span>');
var translationContent = "";
translationContent += 'Testtext <em> test </em>';
translationWrapper.append(translationContent);

If I do this. The em tag isn't interpreted as a tag. Instead its interpreted as text. How can I let it interpret the tag ?

2
  • 1
    jQuery append(..) takes a String, the documentations gives an example $( ".inner" ).append( "<p>Test</p>" );. I ran your code in a fiddle and it seems to work fine. Commented Nov 17, 2015 at 12:38
  • Thanks. It showed me that this doesn't cause the problem. So the problem seems to be in my code. Commented Nov 17, 2015 at 12:45

2 Answers 2

1

Before append you need to convert it to jQuery object

translationContent += 'Testtext <em> test </em>';
translationWrapper.append($(translationContent));
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. Thats exactly what I missed.
@Kbi - If this is your solution, then please accept it.
I cannot accept it until 12 minutes pass. I found one more thing: If i create the variable with translationContent = ""; it does not work. Do I have to generate it as a jQuery object ?
Please repeat question or provide jsfiddle with code
0

The comment from George Lee "solved" my issue. The problem is not in the provided code. It seems to be in my data.

using

jQuery(translationWrapper).append(translationContent); 

works fine

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.