views:

45

answers:

1

Hello,

I got this code which works:

    <html>

    <head>

    <title>JS highlighting test</title>

    <script type="text/javascript">

    function highlight()
    {
            var t = document.getElementById('highlight').innerHTML;

            t = t.replace(/(if|switch|for|while)\s*(\()/gi, "<b>$1</b>$2");
            document.getElementById('highlight').innerHTML = t;
    }

    </script>

    </head>

    <body onload="javascript:highlight();">

    <pre id="highlight">
    1  if(foo) {
    2          bar();
    3  }
    4
    3  while(--baz) {
    5          oof();
    6  }
    </pre>

    </body>

    </html>

I would like to have it for all <pre> tags instead of just one with some specific and unique id as it works now. The best would be to have an combination of a specific tag with a specific id. Is it possible to extend the small JS function above to work this way (using some document.getElementsByTag(tag).getElementsById(id).innerHTML or something alike (I don't know what exactly suites the need) in a loop? I tried myself but with no real success. I need only as simple solution as possible, nothing really special.

Thank you for your ideas.

--
nkd

+2  A: 

You almost guessed it right ;-)

function doSomethingWithAllPres() { 
    var pres = document.getElementsByTagName("pre");
    for (var i = 0; i < pres.length; i++) { 
        // you can check the class name via pres[i].className === "highlight"
        // (and you should use a class instead of an id therefore)
        if (pres[i].className.indexOf("highlight") >= 0) {
            // action goes here
        }
    }
}

Using a JS-Framework like jQuery, things would be even more simple:

$("pre.highlight").each(function(i) {
    // action goes here
});

However, using a framework might be overkill...

Mef
I tried this: <script type="text/javascript"> function highlight() { var pres = document.getElementsByTagName("pre"); for (var i = 0; i < pres.length; i++) { var h = pres[i].innerHtml; h = h.replace(/if/gi, "<b>if</b>"); pres[i].innerHtml = h; } } </script>But it does not work, why?The problem is I got NO idea how to get the replaced content back to the appropriate pre tag when working with the array. That's exactly the problem I got.
nkd
Use innerHTML instead of innerHtml and it will work (be sure to replace both...)
Mef
Fantastic! It works. But, what to do to combine the pre tag condition with the "highlight" id condition? I tried 'document.getElementsByTagName("pre").getElementsById("highlight")' which does not work for me.
nkd
So you want all `pre` s and the element with the `id` "highlight"? If you refactor the actual highlight code into a function (e.g. function highlightNode(node){...}), you can call that for every pre and the highlight element. Check to make sure highlight isn't a `pre` so you don't do it twice on the same node.
Matthew Flaschen
I added this aspect to my answer in the meantime... you should apply a class (not an id) and you can check whether a pre has this class or not easily inside the loop (however you should be aware that a valid classname could also look like `classA hihglight` so you probably should use a more sophisticated test than ===).
Mef
Looks very difficult to me at the moment (this is my first JS code ever). :)
nkd
One last edit to clarify... Now the code applies to all pres wich have the class `highlight` ;-) Now, don't you forget to accept an anwser ;P
Mef
Thank you very much.
nkd
Too early. Hm, it seems the trick with className.indexOf does not work at all.
nkd
Sorry, a "CLASS" name, not "ID"! :) My fault. Everything works now. Thank you very much for fast and good response.
nkd