1
$("#confirm_text").text("Are you sure you want " +this.name +" "+ this.type +"?");

The whole phrase is white, but I want to make this.name and this.type orange. JS method fontcolor doesn't work. Any way to do this?

5
  • Make a fiddle showing what have you tried.. Commented Oct 29, 2013 at 11:05
  • 1
    Wrap this.name and this.type in spans with required classes. Commented Oct 29, 2013 at 11:05
  • 1
    is it a javascript alert message, if yes then you cannot change the style in it. You can use some custom popup to display custom message with css Commented Oct 29, 2013 at 11:05
  • Can you say your question cleary? do you want to add some style rule to element by jQuery or JavaScript? for example set the color: red; to DOM element? Commented Oct 29, 2013 at 11:06
  • I updated question details. Commented Oct 29, 2013 at 11:20

6 Answers 6

2

Try this :

"Are you sure you want <span style='color: orange'>" +this.name +" "+ this.type +"</span> ?"

Here you can see demo : http://jsfiddle.net/2pRzX/

Sign up to request clarification or add additional context in comments.

1 Comment

It works, but when I click on other items that use it, I end up having the text recur many times.
1

this may work.

"Are you sure you want " +<span class="orange">this.name</span> +" "+ <span class="orange">this.type</span> +"?"

and apply css:

.orange{
    color:orange;
}

1 Comment

This is syntactically completely incorrect. It will throw SyntaxError: Unexpected token <.
1

I usually use < span class="orange"> this.name < /span> for this case.
In css .orange{color:orange;}

Comments

1

You have to put the text in some kind of HTML element, like <span>. Either you do it with innerHTML, which is the simpler method, or by creating the elements one by one.


Using jQuery (updated answer)

As the question was updated, I updated my answer to fit the new question.

Instead of .text, you can use the .html method which works like element.innerHTML = (see below in the old answer). It parses HTML code and inserts it into the element. Note that you should enclose the string in single quotes so that you can use double quotes without escaping them.

$("#confirm_text").html('Are you sure you want <span style="color:orange">' + this.name + ' ' + this.type + '?');

Using plain JavaScript (old answer)

This answer is outdated because the question has changed and jQuery rather than plain JS is required. I leave this here for later reference.

innerHTML

When you add HTML code to an existing element in the DOM, the browser will parse the string and create new elements. You can include the <span> tag in the string and the browser will create an element.

var targetDIV = document.getElementById("targetDIV");
// Add the text with innerHTML
targetDIV.innerHTML = 'Are you sure you want <span style="color:orange;">' + this.name + ' ' + this.type + '</span>?';

Creating the elements

Sometimes you can't just use innerHTML, especially if there is already text in the target element. This is an alternative:

var targetDIV = document.getElementById("targetDIV");

// Append the first peace of text
targetDIV.appendChild(document.createTextNode("Are you sure you want "));

// Create the span element
var orange = document.createElement("span");
// Change its text-color
orange.style.color = "orange";
// Add the text to the span
orange.appendChild(document.createTextNode(this.name + " " + this.type));
// Append the span to the target element
targetDIV.appendChild(orange);

// Append the question mark
targetDIV.appendChild("?");

Which to use?

In most cases, you can use either of these methods. I always use the second one, though, because I have the feeling that it is the "cleaner" one. You have more control over what happens, you can change things by adding/removing/changing lines instead of modifying a large string. You also don't have to check whether your HTML is valid. But as I said, both methods work.

Comments

0

Try this:

"Are you sure you want <span style='color: orange'>" +this.name +" "+ this.type +"</span>?"

Comments

0

You can add <span> </span> tags around the name and type values, and add an colour using CSS. Example: http://jsfiddle.net/BAhUG/

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.