So i'm having this issue where everytime i create a new element that my Jscolor function doest work on the new elements, because it needs a unique Id. And i dont know how to pull that off.
I have tried with for loops and so on and cant figure out how. Because in the JS function i need to select a ID, but how do i select a ID that just have been created?
hope anyone can help me with this issue:)
HTML Code:
<p class="half-circle">
<button class="addNoteBtn">
<i class="ion-android-add-circle"></i>
</button>
</p>
JS Code:
createNote = function(){
var stickerEl = document.createElement('div'),
barEl = document.createElement('div'),
color = document.createElement('button'),
colorIcon = document.createElement('i'),
deleteBtn = document.createElement('button'),
deleteBtnIcon = document.createElement('i'),
moveIcon = document.createElement('i'),
colorEl = document.createElement('input'),
textareaEl = document.createElement('textarea');
var transformCSSPosition = "translateX(" + Math.random() * 800 + "px) translateY(" + Math.random() * 400 + "px)";
stickerEl.style.transform = transformCSSPosition;
barEl.classList.add('bar');
stickerEl.classList.add('sticker');
color.classList.add('color');
deleteBtn.classList.add('deleteBtn');
deleteBtnIcon.classList.add('ion-android-delete');
colorIcon.classList.add('ion-android-color-palette');
stickerEl.id = "1";
colorEl.classList.add('jscolor');
colorEl.onchange = function(){update(this.jscolor)};
colorEl.value = "cc66ff";
color.onclick = function() {showColorPicker};
// tilføj til:
stickerEl.append(barEl);
stickerEl.append(color);
stickerEl.append(deleteBtn);
stickerEl.append(colorEl);
stickerEl.appendChild(textareaEl);
color.append(colorIcon);
deleteBtn.append(deleteBtnIcon);
barEl.append(moveIcon);
stickerEl.addEventListener('mousedown', onDragStart, false);
document.body.appendChild(stickerEl);
};
createNote();
addNoteBtnEl = document.querySelector('.addNoteBtn');
addNoteBtnEl.addEventListener('click', createNote, false);
document.addEventListener('mousemove', onDrag, false);
document.addEventListener('mouseup', onDragEnd, false);
function update(jscolor) {
document.getElementById('1').style.backgroundColor = '#' + jscolor;
}