Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic google-code-prettify doesn't work

Is it possible for a button to call a function that would 'prettify' a dynamic <code><pre>? I can't get it to work.

After the page loads, the initial <code> is prettified(?), but when I change it and call prettyPrint() afterwards, it no longer works.

Example: http://jsfiddle.net/uwBjD/2/

Edit: Sorry, I was using a local prettify.js. Updated it, still encountered the same error.

like image 826
GaiusSensei Avatar asked Apr 13 '13 03:04

GaiusSensei


People also ask

What is code prettify?

CodeBeautify is an online Code Beautifier and Code Formatter that allows you to beautify your source code. It also provides lots of tools that help to save developer's time.


1 Answers

Apparently after the code is prettified, an additional class is added which is prettyprinted. Anything with the class of prettyprinted is not re-prettified. You need to remove that class before recalling the function:

$('input[type=button]').click( function() {
    $("#jsExample").text("    var user = 'private'; //Do NOT store your API Key on a script.")
        .parent().removeClass("prettyprinted");

   prettyPrint();
});

http://jsfiddle.net/uwBjD/3/

like image 180
James Montagne Avatar answered Sep 23 '22 03:09

James Montagne