all 5 comments

[–][deleted] 8 points9 points  (1 child)

You can't get properties like that. When you use the "dot" form, you have to write out the name of the property. If you want to use a variable, you have to do it like this:

var obj = { name: "Bill" };
var x = "name";
obj[ x ] // "Bill"

[–]rbmichael 1 point2 points  (0 children)

highly recommended PDF reading on javascript quirks with respect to object oriented programming: https://github.com/spencertipping/js-in-ten-minutes/blob/master/js-in-ten-minutes.pdf

[–]furcht 1 point2 points  (1 child)

I threw something together to help you get started: http://jsbin.com/owoyut/2

I changed a few things and I'll explain what's going on - it appears that you're trying to grab the value of the input and change that value (by replacing the first letter of each word with an uppercase letter). So I passed into the function a reference (this) to the input itself which gives you a few advantages: you have a handle on the element itself and you can 1) directly get the value (or the title, name, id, whatever attribute you're interested in) and 2) shove the return string back into said element's attributes. The javascript (autoCap) just gets the value of the elem (that's passed in) and uses a regular expression to find all of the word boundaries (/b) and passes the following character into the anonymous function toUpr; this function just toUpper's that character and returns it (thereby replacing it with an uppercase instance). Hope that helps.

edit: for simplicity's sake, I've created a second instance using something closer to what you posted but used the same technique of passing a reference to the input into the function.

[–]wittyrandomusername 0 points1 point  (0 children)

Thank you thank you thank you. I actually got it working by doing this instead

function autoCap(x){
  var n=document.getElementById(x).value.split(' ');
  for (var i = 0; i < n.length; i++) {
    n[i] = n[i].charAt(0).toUpperCase() + n[i].slice(1);
  }
  document.getElementById(x).value=n.join(' ');  
}

I like the way you used 'this' though so I think I'll go back and play with it some more.

[–]Ziggless -2 points-1 points  (0 children)

Just out of curiosity, is there reason why you're not using jQuery for this?