Click here to Skip to main content
13,706,244 members
Click here to Skip to main content
Add your own
alternative version

Stats

4.1K views
60 downloads
4 bookmarked
Posted 11 Sep 2018
Licenced CPOL

Flip jQuery Plugin for Previewing Web Content

, 14 Sep 2018
Rate this:
Please Sign up or sign in to vote.
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;
        $(settings.contentArea).css('display', 'block');
        $(_cdiv).css('display', 'none');
        $(_btn).text(settings.flipOffText);
    } else {
        _state = true;
        $(_cdiv).html($(settings.contentArea).val());
        $(_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)"

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

Share

About the Author

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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Questiontransition append beauty time Pin
elwawi14-Sep-18 6:21
memberelwawi14-Sep-18 6:21 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.180920.1 | Last Updated 14 Sep 2018
Article Copyright 2018 by Mike Hankey
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid