JSFiddle - React, Tailwind, and code Playground

by ronilan

HTML

Starts With:
<br/>
<input class="startsWith" type="text">
<br/>Sentance:
<br />
<textarea class="theString" rows="10" disabled="disabled"></textarea>
<br/>
<br/>
<hr>Count: <span class="output"></span>

JavaScript

var app = {

    init: function () {
        $(".startsWith").focus();
        $(".startsWith").on("keyup", function () {
            $(".theString").prop("disabled", false);
            app.countStartsWith($(".theString").val(), $(".startsWith").val());
        });
        $(".theString").on("keyup", function () {
            app.countStartsWith($(".theString").val(), $(".startsWith").val());
        });
    },

    countStartsWith: function (theString, startsWith) {

        var wordArray = theString.split(" "),
            max,
            i,
            j = 0;

        max = wordArray.length;

        for (i = 0; i < max; i++) {
            if (wordArray[i].substr(0, startsWith.length) === startsWith) {
                j++;
            }
        }
        $(".output").text(j);
    }
}


jQuery(function ($) {
    app.init();
});