Click here to Skip to main content
15,885,366 members
Articles / Web Development / HTML
Tip/Trick

Text Comparator Utility using jQuery

Rate me:
Please Sign up or sign in to vote.
4.25/5 (4 votes)
22 Dec 2017CPOL2 min read 13.2K   264   6   4
Develop the text comparator utility using jQuery plug-in, which internally uses Google's diff_match_patch.js library

Introduction

Any reusable component is not only helpful to solve the individual developer's problem but saves time and cost for the whole project/ team. One of the requirements was to build a component which can track the changes made by an admin within the publicly displayed information.

Background

Considering the criticality of such information, it's always necessary for the other admin user/ reviewer to cross-verify the changes and review it properly before publishing it for the intended global audience. Especially, for such scenarios, the usability of any text comparator utility increases to many folds. Here is the working and very simple approach for developing such utility.

Using the Code

The pre-requisites are to download the below mentioned libraries:

  1. jQuery plug in, i.e., PrettyTextDiff and
  2. Google's diff_match_patch library

Add these library references to the intended HTML document as mentioned below:

JavaScript
<script src="Scripts/jquery.pretty-text-diff.min.js"></script>
<script src="Scripts/diff_match_patch.js"></script>

To track the text differences, which include both addition as well as deletion of any text or character, appropriate color coding approach has been implemented using the styles as mentioned below. It can be used within the same HTML document as well as can be kept in a separate CSS file. Here, in this example, light green color has been used for added text and red color for deleted text from the previous version.

#div1 and #div2 styles are used for different purposes, it helps in scrolling the three column values synchronously, explained in the later part of this article.

CSS
<style>
        ins {
            background-color: #c6ffc6;
            text-decoration: none;
        }
        del {
            background-color: #ffc6c6;
        }
        table, h3, input, label {
            margin: 10px;
        }
            table th {
                width: 30%;
            }
        #div1 {
            height: 50px;
        }
        #div2 {
            height: 50px;
        }
</style>

I created a sample HTML document using the table element which has three columns, i.e., Previous Version, Modified Version and to track the difference between the previous two versions in the third column, i.e., Differences.

Here, textarea element has been used to display the text in the first two columns and label element to display the tracked changes as mentioned below:

HTML
<div id="container">
        <h3>Text Comparator Utility
        </h3>
        <table class="table table-striped table-bordered table-hover">
            <thead>
                <tr>
                    <th>Previous Version</th>
                    <th>Modified Version</th>
                    <th>Differences</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <textarea id="div1" class="original" style="width: 100%; height: 500px">
                            The blue fox climbing over the mountain followed by the armed villagers.
                            Village was full of brave soldiers capable to handle any dangerous
                            situation. They were not only helping the villagers but also the
                            wild animals freely roaming within the village and could pose a risk
                            to their lives as well. The blue fox climbing over the mountain
                            followed by the armed villagers. Village was full of brave soldiers
                            capable to handle any dangerous situation. They were not only helping
                            the villagers but also the wild animals freely roaming within the village
                            and could pose a risk to their lives as well. The blue fox climbing over
                            the mountain followed by the armed villagers. Village was full of
                            brave soldiers capable to handle any dangerous situation. They were
                            not only helping the villagers but also the wild animals freely roaming
                            within the village and could pose a risk to their lives as well.
                            The blue fox climbing over the mountain followed by the armed villagers.
                            Village was full of brave soldiers capable
                            to handle any dangerous situation.
                            They were not only helping the villagers but also the wild animals
                            freely roaming within the village and could pose a risk to
                            their lives as well.</textarea>
                    </td>
                    <td>
                        <textarea id="div2" class="changed"
                        style="width: 100%; height: 500px">
                            The yellow fox running across the dusty road followed by the
                            unarmed villagers. Village was full of brave people, capable enough
                            to handle this situation. They are trying to help not only the villagers
                            but also the wild animals freely roaming out of the woods and could pose
                            a risk to their lives as well.
                            The yellow fox running across the dusty road
                            followed by the unarmed villagers. Village was full of brave people,
                            capable enough to handle this situation. They are trying to help not only
                            the villagers but also the wild animals freely roaming out of the woods
                            and could pose a risk to their lives as well. The yellow fox running
                            across the dusty road followed by the unarmed villagers. Village was
                            full of brave people, capable enough to handle this situation. They are
                            trying to help not only the villagers but also the wild animals
                            freely roaming out of the woods and
                            could pose a risk to their lives as well.
                            The yellow fox running across the dusty road followed
                            by the unarmed villagers.
                            Village was full of brave people,
                            capable enough to handle this situation.
                            They are trying to help not only the villagers but also the wild animals
                            freely roaming out of the woods and
                            could pose a risk to their lives as well.
                            </textarea>
                    </td>
                    <td>
                        <div>
                            <label id="div3" style="width: 80%;
                            max-height: 500px; overflow-y: auto" class="diff"></label>
                        </div>
                    </td>

                </tr>
            </tbody>
        </table>
        <div>
            <input type='button' class='btn btn-primary' value='Compare' />
        </div>
</div>

input button element to trigger the jQuery/ JavaScript function as mentioned below:

JavaScript
<script>
        $("input[type=button]").click(function () {
            $("#container tr").prettyTextDiff();
        });
</script>

Now, the last functionality to cover the sync scrolling for all the three columns using the below script. It can be combined with the above jQuery function or can be kept as a separate js file.

JavaScript
<script>
        $(document).ready(function () {
            $("#div1").scroll(function () {
                $("#div2").scrollTop($("#div1").scrollTop());
                $("#div3").scrollTop($("#div1").scrollTop());
            });
            $("#div2").scroll(function () {
                $("#div1").scrollTop($("#div2").scrollTop());
                $("#div3").scrollTop($("#div2").scrollTop());
            });
            $("#div3").scroll(function () {
                $("#div1").scrollTop($("#div3").scrollTop());
                $("#div2").scrollTop($("#div3").scrollTop());
            });
        });
</script>

Here, for each overflown column scrolling, the other two columns are bound to reflect the position changes, with the only limitation, if the first column value text length is smaller than the other columns, then other columns have to be scrolled further from their current position. It works perfectly if all the column values are nearly of the same size without much major variations in text length.

Isn't it simple to implement? Keep coding! :)

License

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


Written By
Architect
Singapore Singapore
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Suggestionpost a demo link Pin
Mou_kol18-Dec-17 21:01
Mou_kol18-Dec-17 21:01 
GeneralRe: post a demo link Pin
Abhishek Shrotriya19-Dec-17 20:51
Abhishek Shrotriya19-Dec-17 20:51 
GeneralRe: post a demo link Pin
Dewey20-Dec-17 12:33
Dewey20-Dec-17 12:33 
GeneralRe: post a demo link Pin
Abhishek Shrotriya20-Dec-17 18:59
Abhishek Shrotriya20-Dec-17 18:59 

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.