0

I was simply wondering how I would add html code inside a javascript variable? For example, I want to use HTML code (a <a href="/example.html"> link piece of code) in one of the message lines:

var messages = [
"Please wait while we handle your request...",
"This is taking longer than usual to process - please wait...",
"We seem to be having trouble - please click here to contact support"
];

$.each(messages, function(index, message) {
setTimeout(function() {
    $('#wrapper').append(
        $('<p />', {text : message})
    )
}, index * 1500); // 1.5 seconds, add a zero for 15 seconds
});

So it would be like:

var messages = [
"Please wait while we handle your request...",
"This is taking longer than usual to process - please wait...",
"We seem to be having trouble - please click <a href="/help.html">here</a> to contact support"`

Is there a way of doing this?

1
  • Yes, it is possible, you just need to fix the quotes, and use html instead of text. Commented Aug 3, 2014 at 19:16

1 Answer 1

1

Of course, you have to:

  1. Fix the quotes (") in the string escaping them (\").
  2. Use html: instead of text:.

First, edit your messages variable like this:

var messages = [
    "Please wait while we handle your request...",
    "This is taking longer than usual to process - please wait...",
    "We seem to be having trouble - please click <a href=\"/help.html\">here</a> to contact support"
];

Then edit your code to create the <p> element like this:

 $('<p />', {html : message});
Sign up to request clarification or add additional context in comments.

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.