JSFiddle - React, Tailwind, and code Playground

HTML

<br/>
<label for="fname">First Name: <input type="text" name="fname" /></label>
<br/><br/>
<label for="lname">Last Name: <input type="text" name="lname" /></label>

JavaScript

$('input').keypress(function(e) {
    var $t = $(this);
        key = e.keyCode,
        txt = $t.val().split(' '),
        last = (txt.length - 1);
        lword = txt[last];
    
    

    function replace() {
        txt[last] = lword;
        $t.val(txt.join(' '));
    }

    function undo() {
        lword = lword.toLowerCase();
        replace();
    }

    function capitalize(n) {
    alert();
        lword = lword.charAt(n).toUpperCase() + lword.slice(n+1);
        replace();
    }

    if (key === 8) {
        undo();
    } else if (key === 32) {
        capitalize(0);
    }
    
});