Click here to Skip to main content
15,867,330 members
Articles / Web Development
Tip/Trick

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 11.6K   105   6   2
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.

JavaScript
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:

HTML
<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:

JavaScript
$(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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Retired
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
elwawi14-Sep-18 6:21 
AnswerRe: transition append beauty time Pin
Mike Hankey25-Sep-18 3:33
mveMike Hankey25-Sep-18 3:33 
Thanks for the input!

I've added the animate option to the plugin and updated the tip.
Everyone has a photographic memory; some just don't have film. Steven Wright

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.