Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Flip jQuery Plugin for Previewing Web Content

0.00/5 (No votes)
25 Sep 2018 1  
The Flip jQuery plugin is a light weight extension that allows the user to type raw HTML and CSS in a textarea and have the ability to preview the formatted content without reloading page.

My web site has been offline for a year while I took a sabbatical and volunteered at state parks around Florida. The trade off for living in a wilderness environment is that internet when you can get it, via hotspot is terrible at best. During my year off, I wasn't able to maintain my site in a manner I would have liked so I let the lease lapse and just enjoyed my time with nature. But now, I'm back in civilization and working hard to get a site up and in the process, I'm completely rewriting it using ASP.NET MVC WebApi, taking my time and adding all the bells and whistles I didn't implement in my previous site. It was getting pretty stale anyway as I didn't keep it up like I should have.

Early in the rewrite, I was using CKEditor for composing my posts, but it is so blotted and difficult to tame that I decided to just use a textarea and enter raw HTML and CSS directly, since that is what I was pretty much doing in CKEditor anyway. (I must admit though that I write my code in Visual Studio Code, then copy and paste into the textarea, I do this because the VS Code editor has intellisense and it makes writing HTML a lot easier and faster.) This worked very well, but a problem I had was a way to preview the modifications that I had made without reloading the page, which would have required saving the content each time I wanted to preview. What I came up with, is in my opinion, a slick way of solving this problem, a simple jQuery plugin called Flip that "flips" between code and preview modes without reloading the page.

The first image below shows the raw code in the textarea, with the Flip button just above the textarea to the left.

Figure 1. Flip showing raw code.

This next image shows the formatted code in an uneditable area, a div to be exact. Notice that the "Flip" button's text has changed.

Figure 2. Flip showing formatted code.

Now into the mechanics of the plugin. The working section of the code, the "Flip" button click event handler is shown in the code segment that follows. As the comment says, its only function is to toggle the visibility of the textarea containing the raw text and the div that displays the formatted text. The reference to Prism is used to invoke the Prism syntax highlighter if it is present and to ignore if not. I use it because it is lightweight and easy to use.

function onClick() {
    //This is the heart of the plugin.  It just toggles the visibility of the content
    //  and formatted code div areas.
    if (_state) {
        _state = false;
        if (settings.animate) {
            $(_cdiv).css('display', 'none');
            $(settings.contentArea).fadeIn(1000, 'linear');
        } else {
            $(settings.contentArea).css('display', 'block');
            $(_cdiv).css('display', 'none');
        }
        $(_btn).text(settings.flipOffText);
    } else {
        _state = true;
        $(_cdiv).html($(settings.contentArea).val());
        if (settings.animate) {
            $(settings.contentArea).css('display', 'none');
            $(_cdiv).fadeIn(1000, 'linear');
        } else {
            $(_cdiv).css('display', 'block');
            $(settings.contentArea).css('display', 'none');
        }
        $(_btn).text(settings.flipOnText);
        //If Prism syntax highlighter is present invoke it, if not ignore.
        if (typeof Prism !== 'undefined') Prism.highlightAll();
    }
};

 

Flip options are limited at this point, the table below describes the ones that I have provided to date.

Option Description Default
contentArea textarea containing the raw data null
flipOffText Text to be displayed on button when in raw mode "Flip"
flipOnText Text to be displayed on button when in formatted mode "Flip (Source)"
animate* Apply an anuimation to the transition false

Thanks to elwawi for the animation tip.  I've updated the tip and code with the animation option!

Using Flip is pretty easy, in your HTML, add the code below:

<div id="pcon">
    <textarea name="Content1" id="content">...some text here...</textarea>
</div>    

Then for the JavaScript, add the following referring to the id of the textarea:

$(function () {
    $('#pcon').flip({ contentArea: '#content' });
});    

I realize that this is a crude way of creating and displaying HTML text but it works for me and just maybe someone else is looking for a lightweight, easy to use plugin to preview their data. Using a HTML editor like CKEditor is nice and has a lot of bells and whistles, but I didn't need all that blot.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here