Create runnable snippets on your Wordpress blog

Posted on

Here’s how to create a runnable snippet on your WordPress blog.

$(function() {
    $('.js-runnable').each(function(){
        var that = this;
        var button = $('<p><button class="btn btn-default">Run Snippet</button></p>');
        $(button).find('button').click(function(){
            eval($(that).text());
        });
        $(this).after(button);
    });
});

You need to create a pre element with the class .js-runnable. The upper script will the automatically add a button after the code to run the code inside the snippet.

Demonstration:

!(function(){
    var a = 'World';
    alert('Hello '+a);
})();

Leave a Reply