2

So, I was looking how to do something similar to Stackoverflow's code thing:

e.g. The "var" is colored and "test" isn't

var test

How would I write a JavaScript function to do this?

This is what I tried

if ("var")  {
document.getElementByTagName("code").style.color="purple";
}

So when the text "var" is in a "code" element, it turns JUST "var" purple.

3
  • 1
    You are in a wrong way right now. You should improve your html and javascript knowledge first before trying do this thing , because you are stucked in wrong concepts that im my option will retard your learning. Commented Nov 6, 2013 at 1:37
  • @Wagner Thanks, I'm pretty familiar with HTML and CSS, I was just wondering if there was a way to do it faster with JavaScript. Commented Nov 6, 2013 at 1:42
  • If you're actually looking to do proper syntax highlighting, it's a lot more complex than that. It would either have to be a very complicated regular expression (for most common forms of syntax - still doesn't work fully) or you'd have to tokenise your characters into an AST and map the relevant parts to the original code accordingly. Commented Nov 6, 2013 at 2:41

3 Answers 3

2

I would wrap the variable name in a span

<code>
  var <span class="variable-name">test</span>
</code>

Then color just the span, jQuery makes this easier

$('code span.variable-name').css('color', 'purple');

However, There are also syntax highlighting libraries that would make this whole thing easier since it's largely a solved problem :) Here is an example

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

4 Comments

Doing it yourself would require implementing a decent chunk of a lexer, so a library is definitely the way to go.
I was looking to do it without an extra HTML. Is this possible?
Certainly possible, I would look into a syntax highlighting library like: craig.is/making/rainbows
Thanks for accepting, I assume you accepted because of my comment above. ^
0

Here's a quick and dirty (emphasis on dirty) way to do it with regex replace if:

  1. You don't care about actually parsing the syntax
  2. You know the code elements only contain clean text, no child tags

If that's so, you can do something like:

var els = document.getElementsByTagName('code'),
    code;

for (var i = 0; i < els.length; i++) {
    code = els[i].innerHTML;
    els[i].innerHTML = code.replace(
        /\bvar\b/g,
        '<span style="color: #f00;">var</span>'
    );
}

Yes, it's better practice to use a class instead of inline style, but seeing as not following the above two points is also bad practice, this is just for example's sake.

See demo

Comments

0

HTML and js element

 <div>
   var test;
 </div>


<script>
 var varText = $( "div" ).text().split( " " ).join( "</span> <span>" );
 newText = "<span>" + varText + "</span>";

$( "div" )
.html( varText )
.find( "span" )
.end()
.find( ":contains('var')" )
.css({
  "color": "purple",

});

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.