0

I want to apply font color to the string value in JavaScript. How do I?
I tried like below: I want the search text in red color.

$(document).ready(function() {
    var defaultText="search";
    defaultText.text.color="red";
});
9
  • 2
    Where you want to put it then? Show expected HTML. Commented Apr 22, 2013 at 6:43
  • Welcome to SO. Although your question isn't quite according the SO standards (try to read the rules), I think @chandresh_cool provided the correct answer. Commented Apr 22, 2013 at 6:46
  • @Baszz did you try it? It is not correct. Commented Apr 22, 2013 at 6:47
  • defaultText is a string, you cannot change the color of a string value Commented Apr 22, 2013 at 6:51
  • @ArtyomNeustroev No, I did not...and you are right. I assumed it was some kind of DOM node. The way it is in the question, it will not work indeed :) Commented Apr 22, 2013 at 6:51

2 Answers 2

1

you can only change the color of DOM element.In your case it is not DOM variable.It is a string you are not showing any where in the html.So it is not possible to apply color to that string .If you apply color also where you will see that color.

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

3 Comments

I guess the downvote is for the un-edited answer. anyway your answer should really be only a comment...
Why did you format your whole answer as a quote? Please pay more attention to formatting, you better read this as well.
Actually there is search box,i want to add the 'search' text inside that box,i did that.now i wanna change the color of that text.can anybody help
0

You can use span for holding string search and apply css to this span.

<span id="search"></span>

$(document).ready(function() {
    $('#search').val('search').css('color','red');
});

1 Comment

Actually there is search box,i want to add the 'search' text inside that box,i did that.now i wanna change the color of that text.can anybody help

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.