I have a script that prevents the user from pasting, or dropping dragged images into a contenteditable area. When the user tries to do one of those two things, I want an alert to tell him he cannot do that. I tried adding alert("custom message") right before the last break. But it gets triggered in both cases (pasting and dropping). How can I have two different alerts: one for when the user pastes an image ("you cannot paste images here"), and another for when the user drops a dragged image ("you cannot drop images here", within my code?
https://jsfiddle.net/36cfdpoh/
window.onload = function() {
document.getElementById("editDiv").oninput = function() {
var thisElement = this;
var currentElementChildren, currentElementChildrenLength;
var elements = this.children;
var elementsLength = elements.length;
if (elementsLength > 0) {
for (i = 0; i < elementsLength; i++) {
if (elements[i].tagName == "IMG") {
thisElement.children[i].remove();
break;
} else {
currentElementChildren = elements[i].children;
currentElementChildrenLength = currentElementChildren.length;
if (currentElementChildrenLength > 0) {
for (ii = 0; ii < currentElementChildrenLength; ii++) {
if (currentElementChildren[ii].tagName == "IMG") {
thisElement.children[i].children[ii].remove();
alert("you cannot drop images here");
break;
}
}
}
}
}
}
}
}
#editDiv {
width: 250px;
height: 50px;
border: solid 1px #000;
margin-bottom: 20px;
}
img {
width: 250px;
}
<div id="editDiv" contenteditable="true">
<p>Try to drop or paste that image here.</p>
</div>
<img src="https://i7.pngguru.com/preview/134/138/533/star-golden-stars.jpg">