Click here to Skip to main content
15,392,679 members
Articles / Web Development
Posted 11 Sep 2018


6 bookmarked

Flip jQuery Plugin for Previewing Web Content

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
25 Sep 2018CPOL3 min read
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.

Image 1

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.

Image 2

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');
    } else {
        _state = true;
        if (settings.animate) {
            $(settings.contentArea).css('display', 'none');
            $(_cdiv).fadeIn(1000, 'linear');
        } else {
            $(_cdiv).css('display', 'block');
            $(settings.contentArea).css('display', 'none');
        //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>

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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mike Hankey
United States United States
Currently enjoying retirement and working on projects without pressure, deadlines or any kind of management.

Comments and Discussions

Questiontransition append beauty time Pin
elwawi14-Sep-18 6:21
Memberelwawi14-Sep-18 6:21 
AnswerRe: transition append beauty time Pin
Mike Hankey25-Sep-18 3:33
professionalMike Hankey25-Sep-18 3:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.