I saw a lot of posts about this pattern, but still don’t understand why it’s not work in mine code. Using Chromium on Ubuntu 10.04.
function showDescription(){
var description = document.createElement('div'),
eventTarget = window.event.target,
newHTML = description.innerHTML;
description.id = 'description';
switch(eventTarget.getAttribute('data-par')){
case 'links': newHTML = newHTML.replace(newHTML, 'links')
break;
case 'images': newHTML = newHTML.replace(newHTML, 'images')
break;
};
console.log(newHTML);
parentElement.insertBefore(description, parentElement.firstChild.nextSibling);
};
var descParLi = descPars.getElementsByTagName('li');
for (var i = 0; i < descParLi.length; i++){
descParLi[i].addEventListener("click", showDescription);
};
This code contained in other function. Strangely, in console I see value of newHTML, but in html have no changes, that’s a problem. What is wrong? How to fix?
You aren’t adding the content to the actual
divyou created.There’s no point in doing:
…or:
…because
descriptionis a brand new element, so there isn’t any innerHTML content yet.EDIT: I used
divinstead ofdescriptionfor the variable name above. It should usedescriptioninstead:Fixed.
Here’s the entire function:
Notice I added an incrementing
iso that your ID will be unique each time.This won’t be necessary if you’re removing the element before another one is shown.
Also, you have a
parentElement. I don’t know what that’s supposed to reference.If it is some appropriate variable not shown, then you’re fine, but if it should be a reference to the parent of the
target, then you should do this instead: