0

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?

Geek Num 88
  • 5,264
  • 2
  • 22
  • 35
Matthew Walker
  • 193
  • 1
  • 15

0 Answers0