By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,939 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,939 IT Pros & Developers. It's quick & easy.

color the line begin with

P: 104
In html/css/javascript, is it possible to change the color of the line for which you set certain parameters like if the line begin with # , then change with color of the line blue

This is normal line
#This line should automatically get blue via javascript or whatever
the next line is again normal line

I hope you get my point, any help is much appreciated

Apr 9 '08 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 15k+
P: 16,027
Yes, this should be possible if you take the text and split using newlines, then check the first character and add spans around the content that needs to be colored blue, then finally write this content back to the content holding element.
Apr 9 '08 #2

P: 104
I tried but I couldn't get it working, if you can give an example here as I'm not good at scripting stuff.
Apr 9 '08 #3

Expert Mod 15k+
P: 16,027
An example to give you an idea:
Expand|Select|Wrap|Line Numbers
  1. <style>
  2. .blue { color: blue; }
  3. </style>
  4. <script>
  5. function colorBlue() {
  6.   var cd = document.getElementById("content");
  7.   var content = cd.innerHTML.split("\n");
  8.   for (i = 0; i < content.length; i++) {
  9.     if (content[i].charAt(0) == "#") {
  10.       content[i] = "<span class='blue'>"+content[i]+"</span>";
  11.     }
  12.   }
  13.   cd.innerHTML = content.join("\n");
  14. }
  15. window.onload = colorBlue;
I've split on the newline, but if you mean a newline in HTML output, you could split on "<br>". I've also assumed a div named "content" - you could change this to document.body.
Apr 10 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.