I want to capitalise the first letter of a string. That part is done.
I can capitalise div.title elements and other elements with no child nodes no proble.
What i want to do it call the text node and change only the first text node.
Therefore I would like to convert all fcn objects to uppercase.
I have the following html:
<div class="title">Me & you
<div class="subtitle">Me & you</div>
</div>
<br />
<div class="title">Me & you</div>
and the following Jquery:
function capitalise(str) {
if (!str) return;
var stopWords = ['a', 'an', 'and', 'at', 'but', 'by', 'far', 'from', 'if', 'into', 'of', 'off', 'on', 'or', 'so', 'the', 'to', 'up'];
str = str.replace(/\b\w+\b/ig, function(match) {
console.log(match)
return $.inArray(match, stopWords) == -1 ? match.substr(0, 1).toUpperCase() + match.substr(1) : match;
});
return str;
}
//Capitalise subtitles
$('div.subtitle').each(function() {
$(this).text(capitalise($(this).text()));
});
//Capitalise Titles with no children
$('div.title').filter(function() {
return !$(this).children().length;
}).each(function() {
$(this).text(capitalise($(this).text()));
});
var fcn = $('div.title').contents().filter(function() { return this.nodeType === 3;});
alert(fcn);
console.log(fcn);
I want to capitalise the fcn variable array. Tried various methods without success.
http://jsfiddle.net/bizwizone/wMCEa/1/
Any thoughts?