color the line begin with

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • usafshah
    New Member
    • Nov 2006
    • 104

    color the line begin with

    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

    example:
    =============== =============== =============== ===
    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

    Thanks.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    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.

    Comment

    • usafshah
      New Member
      • Nov 2006
      • 104

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

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        An example to give you an idea:
        [code=javascript]<style>
        .blue { color: blue; }
        </style>
        <script>
        function colorBlue() {
        var cd = document.getEle mentById("conte nt");
        var content = cd.innerHTML.sp lit("\n");
        for (i = 0; i < content.length; i++) {
        if (content[i].charAt(0) == "#") {
        content[i] = "<span class='blue'>"+ content[i]+"</span>";
        }
        }
        cd.innerHTML = content.join("\ n");
        }
        window.onload = colorBlue;[/code]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.

        Comment

        Working...