JSFiddle - React, Tailwind, and code Playground

by Jose Serna

HTML

<body>
    <div class="container">
        <p class="target-paragraph">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
        <p class="button">Submit</p>
        <div class="word-bank">
             <h2>Your Selected Words Are</h2>

            <ul class="selected-words"></ul>
        </div>
    </div>
</body>

CSS

.underlined {
    text-decoration: underline;
}
.container {
    width: 50%;
    margin: 20px auto;
}
.button {
    width: 50px;
    padding: 20px 30px;
    border: 2px solid black;
    margin: auto;
}
.button:hover, span:hover {
    cursor: pointer;
}
.selected-words {
    list-style: none;
}
.selected-words li {
    display: inline-block;
    padding: 10px;
}
.word-bank {
    display: none;
    text-align: center;
}

JavaScript

var text = $('p.target-paragraph').text();
var $words = text.split(" ");
var $newText = "";
$.each($words, function (i, val) {
    $newText = $newText + '<span>' + val + '</span> ';
})
$(".target-paragraph").html($newText);
//Turn each word into a span.
//if click on span. Add underline class.
//reclicking on the word will cause it to lose its underline class.
$('span').click(function () {
    $(this).toggleClass('underlined');
});
//on submit create POST array containing everyone underlined word
$('.button').click(function () {
    var $selectedWords = [];
    $('.underlined').each(function () {
        var $word = $(this).text();
        if ($word.substr(-1) === '.') {
            $word = $word.slice(0, -1)
        }
        $selectedWords.push($word);
    });
    //hide the original paragraph text and submit button
    $('.target-paragraph').hide();
    $('.button').hide();
    $.each($selectedWords, function (i, val) {
        $('.selected-words').append('<li>' + val + '</li>');
    })
    $('.word-bank').show();
})