Reputation: 33
I ran into a weird problem, input box html not allowing typing.
const para = document.createElement('p')
const innerCard = document.getElementsByClassName('attach')
for(let i = 0; i < innerCard.length; i ++){
innerCard[i].addEventListener('click',createInput)
innerCard[i].addEventListener('dblclick',hide)
}
function hide(){
para.style.display = 'none'
for(let i = 0; i < innerCard.length; i ++){
innerCard[i].removeEventListener('mouseout',hide)
}
}
function createInput(){
let input = document.createElement('input');
para.innerText = 'Topics'
para.className = "blem'
innerCard[0].appendChild(para)
para.appendChild(input)
para.style.display = 'grid'
for(let i = 0; i < innerCard.length; i ++){
innerCard[i].removeEventListener('mouseover',createInput)
}
}
full code: https://jsfiddle.net/nj7ne83y/3/
Upvotes: 0
Views: 46
Reputation: 1295
I got it working, with the assumption that you want to create a sibling of the class "attach", not a child of it.
Change this line:
innerCard[0].appendChild(para)
to this:
innerCard[0].parentElement.appendChild(para)
Upvotes: 0
Reputation: 18378
Your click
on input propagates to innerCard
and calls createInput
over and over again.
Stop it this way:
function createInput(){
let input = document.createElement('input');
input.addEventListener('click', function(e){e.stopPropagation()});
...
https://jsfiddle.net/nj7ne83y/4/
Upvotes: 1