1

I'm using Prettify for syntax highlighting, but it doesn't work on dynamically generated code.

I have a form that when submitted generates code and displays it (without refreshing) in <div id="output></div>, but prettify doesn't work on this code, is there any workaround?

Many thanks!

2
  • How are you calling prettify? Commented Apr 4, 2010 at 4:13
  • Not exactly answer-worthy, but, try <div id="output"></div> - with the second quote-marks or whatever they're called (it's missed out from the other side of output) Commented Apr 4, 2010 at 17:20

2 Answers 2

1

Make sure you recall Prettify once the new code is loaded.

You will need to add a handler to the 'Submit' event. I don't know if you are using a framework or raw JS, so I can't give a code example.

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

Comments

1

There's a nice solution at http://www.codingthewheel.com/archives/syntax-highlighting-stackoverflow-google-prettify.

In short:

  • listen for an event: user is inactive for X seconds (after key press)
  • run the Prettify function prettyPrint()
  • (if a user starts typing before X seconds do not run Prettify)

.

$(document).ready(function() {
  $('#mytextarea').keydown(function() {
    $(this).stopTime();
    $(this).oneTime(1000, function() { 
      /* launch the Prettify here */
    });
  });
});

code copied from http://www.codingthewheel.com/archives/syntax-highlighting-stackoverflow-google-prettify

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.