JSFiddle - React, Tailwind, and code Playground
HTML
<div class="test">first test</div><br />
<div class="test">second test</div><br />
<div class="test">third test</div><br />
<div class="test">fourth test</div><br />
JavaScript
$.fn.capitalise = function() {
return this.each(function() {
var $this = $(this),
text = $this.text(),
tokens = text.split(" ").filter(function(t) {return t != ""; }),
res = [],
i,
len,
component;
for (i = 0, len = tokens.length; i < len; i++) {
component = tokens[i];
res.push(component.substring(0, 1).toUpperCase());
res.push(component.substring(1));
res.push(" "); // put space back in
}
$this.text(res.join(""));
});
};
$(".test").capitalise();