0

I have some javascript code that users the ternary operator to determine the first part of a message variable.

response.answer == "stack overflow" ? message = "blah blah some message." : message = "blah blah some other message" + some variable; 

Once that ternary operator's complete, I then concatenate more onto the string.

message += " About me: my name's Blah blah. Contact me on @blah blah Twitter. This game was made with blah blah."

How would you add a span class to the string contained in the message variable so that it can be styled?

1
  • 1
    Please refine your question... what do you mean by "span class"? Commented Jan 28, 2013 at 5:22

2 Answers 2

3

Create a new element and set it's innerHTML to the message.

var span = document.createElement('span');

span.innerHTML = message;
span.className = 'myClass';

document.getElementById('targetContainer').appendChild(span);
Sign up to request clarification or add additional context in comments.

Comments

0

if you mean to add a span tag with a class to final message variable, then you can do like this

message += " Abou ..... "; // after this line add
message = "<span class='yourclassname'>" + message + "</span>";

then you can add this message html to your targert container as

document.getElementById("targetContainer").innerHTML = message;

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.