I have a for
loop which creates some DOM Elements using a function. The for loop executed with success several times, but suddenly an error appeared and I don't understand why.
I added some console.log
in my code for helping you to understand, I will add a screenshot of the console at the end of my question.
So here is the first part of the code:
if(comments.length > 0) {
for(let k = 0; k < comments.length; k++) {
console.log('in the for 1')
createComment(commentContainer,comments[k])
console.log('in the for 2')
}
console.log('after the for') // We never reach this console.log
}
Then here is the createComments()
function:
let createComment = (container, data, answer = true) => {
console.log('Start createComment')
let newCommentPart = container.getElementsByTagName('textarea')[0]
let singleCommentContainer = createNodeBefore(container, newCommentPart, 'div', '')
if(answer === true){
let answeringContainer = createNode(singleCommentContainer, 'span', '')
let answeringButton = createNode(singleCommentContainer, 'span', `text random`)
if(data.answers && data.answers.length > 0) {
for(let i = 0; i < data.answers.length; i++) {
createComment(answeringContainer, data.answers[i], false)
}
}
let answerPostCommentInput = createNode(answeringContainer, 'textarea', '')
let answerPostCommentButton = createNode(answeringContainer, 'span', 'Some text')
}
console.log('End createComment')
}
And finally, this the createNode()
and the createNodeBefore()
function I use to create my elements:
let createNode = (parent, type, text, id = false, className = false, style = false) => {
console.log('createNode :' + type)
let textNode = document.createTextNode(text)
let element = document.createElement(type)
element.className = className ? className : "";
element.id = id ? id : "";
element.style = style ? style : "";
element.appendChild(textNode)
parent.appendChild(element)
console.log('Element :' + element)
return element
}
let createNodeBefore = (parent, node, type, text, id = false, className = false, style = false) => {
let textNode = document.createTextNode(text)
let element = document.createElement(type)
element.className = className ? className : ""
element.id = id ? id : ""
element.style = style ? style : ""
element.appendChild(textNode)
parent.insertBefore(element, node)
return element
}
So the function is executed correctly multiple time, but for some reason it stop working. By looking closer, I was able to determine that the part of the code which not working is this part of the createComment()
function :
let createComment = (container, data, answer = true) => {
console.log('Start createComment')
let newCommentPart = container.getElementsByTagName('textarea')[0]
console.log(newCommentPart)
console.log(container)
let singleCommentContainer = createNodeBefore(container, newCommentPart, 'div', '')
console.log('We never reach this part')
Another really weird thing is, in the createComment()
function, if I replace
let answerPostCommentInput = createNode(answeringContainer, 'textarea', '')
By
let answerPostCommentInput = createNode(answeringContainer, 'span', '')
everything works and I didn't have any error.
Don't hesitate to ask me if you need more details or if something is unclear.
EDIT
As requested, I add a console.log of the arguments of the createNode before function, everything seems ok