The following example should do the trick! (see a working JSFiddle here).
You shouldn't use for..in
with array-like objects as the order cannot be guaranteed. Also when the value of an element is referenced in a dynamic function you need to use this.value
.
var elems = document.getElementsByTagName("textarea")
matchClass = "uiTextareaAutogrow";
for(var i = 0, n = elems.length; i < n; i++) { // <---- simple for loop
if((' ' + elems[i].className + ' ').indexOf(' ' + matchClass + ' ')> -1) {
elems[i].onkeyup = function(evt) {
if(evt.keyCode == 13){
alert(this.value);
}
}
}
}