I'm trying to create a capitalization function using prototyping but it only works with text strings and not injected variables. I don't know what I need to change to make it work.
String.prototype.capitalize = function() {
return this.replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); });
};
That is my prototype but if i use it with var addition = document.getElementById('addition').capitalize();
it returns:
TypeError: document.getElementById('addition').capitalize is not a function. (In 'document.getElementById('addition').capitalize()', 'document.getElementById('addition').capitalize' is undefined)
Any thoughts as to how I can change this?