i'm trying to run my JS code in differend areas independently, so i wanted to create a class and found this article and my problem is, that I can't use functions, which are in my class in my class.
Here is my JS Code:
window.addEventListener('load', tagEditorsInit);
function tagEditorsInit() {
var tagEditors;
var tagLists = document.getElementsByClassName('tagList');
for(var i = 0; i < tagLists.length; i++) {
tagEditors[i] = new function() {
var edit, editTag, tagList, tagInput, tagOutput, i;
this.init = new function() {
edit = false;
tagList = document.querySelectorAll('[class=tagList]').item(i); //L.96/97 #1
tagInput = tagList.querySelectorAll('[name=tagInput]');
tagOutput = tagList.querySelectorAll('[name=tags]');
tagInput.addEventListener('keyup', tagOnKeyPress(event.keyCode)); //13-ERROR
tagList.addEventListener('mouseout', function() {if(tagList.ownerDocument.activeElement.parentNode !== tagList && !isHover(tagList)) {tagList.style.maxHeight = '40px';}});
tagInput.addEventListener('focus', changeSize);
tagInput.addEventListener('blur', changeSize);
for(var i = 2; i < tagList.childNodes.length; i++) {
tagList.childNodes[i].addEventListener('click', tagOnClick);
tagList.childNodes[i].addEventListener('mousemove', tagOnMouseMove);
tagList.childNodes[i].addEventListener('mouseout', tagOnMouseOut);
}
};
this.tagOnKeyPress = new function(keyCode) {
var tagInputValue = tagInput.value.trim();
if(tagInputValue.length !== 0) {
if(edit) {
if(keyCode === 13) {
edit = false;
editTag.style.borderColor = '#ddd';
tagInput.value = '';
return;
}
tagOutput.value = tagOutput.value.replace(editTag.innerHTML,tagInputValue);
newTag = editTag;
} else {
if(keyCode !== 13) return;
tagOutput.value = tagOutput.value + tagInputValue + ';';
var newTag = document.createElement('div');
newTag.addEventListener('click', tagOnClick);
newTag.addEventListener('mousemove', tagOnMouseMove);
newTag.addEventListener('mouseout', tagOnMouseOut);
}
newTag.innerHTML = tagInputValue;
tagList.appendChild(newTag);
}
if(!edit) tagInput.value = '';
};
this.tagOnClick = new function() {
if((this.offsetWidth + getOffsetLeft(this) - event.pageX) < parseInt(this.style.backgroundSize, 10)) {
tagOutput.value = tagOutput.value.replace(this.innerHTML + ';','');
this.parentNode.removeChild(this);
tagInput.value = '';
edit = false;
} else {
setEdit(this);
}
tagInput.focus();
};
this.tagOnMouseMove = new function() {
if((this.offsetWidth + getOffsetLeft(this) - event.pageX) < parseInt(this.style.backgroundSize, 10)) {
this.style.backgroundSize = '16px';
} else {
this.style.backgroundSize = '18px';
}
};
this.tagOnMouseOut = new function() {
this.style.backgroundSize = '18px';
};
this.setEdit = new function(tag) {
edit = true;
editTag = tag;
tag.style.borderColor = '#297CCF';
tagInput.value = tag.innerHTML;
};
this.changeSize = new function(e) {
if(e.type === 'focus') {
tagList.style.maxHeight = 'none';
} else if(e.type === 'blur' && !isHover(tagList)) {
tagList.style.maxHeight = '40px';
}
};
this.isHover = new function(elem) {
return (elem.parentElement.querySelector(':hover') === elem);
};
this.getOffsetLeft = new function(elem) {
var offsetLeft = 0;
while(true) { //521px over while scrolling to the right ---> (-TABLE:521px)
if(elem.nodeName !== 'TABLE') offsetLeft += elem.offsetLeft;
elem = elem.parentNode;
if(elem.nodeName === 'HTML') break;
}
return offsetLeft;
};
};
//tagEditors[i].tagList = tagLists.item(i); Why isn't it working??? #1
tagEditors[i].i = i;
tagEditors[i].init();
}
}
I'm getting this error message:
13: Uncaught ReferenceError: tagOnKeyPress is not defined
13: (anonymous function)
8 : (anonymous function)
6 : tagEditorsInit
Questions:
- Can I fix this? ---> How?
- Is there a better way to do this?
Thank you! -Minding