I am working on a drag n drop files thing in HTML and javascript. It makes a new div for every file that is dropped and I want to happen, when it is clicked, a popup shows up. Done with this code.
document.querySelectorAll(".select").forEach(inputElement =>{
const dropElement = inputElement.closest(".dropfiles");
dropElement.addEventListener("dragover", (e) => {
e.preventDefault();
dropElement.classList.add("dropfiles--over");
});
dropElement.addEventListener("click", e => {
inputElement.click();
});
["dragleave", "dragend"].forEach(type => {
dropElement.addEventListener(type, (e) =>{
e.preventDefault();
dropElement.classList.remove("dropfiles--over");
});
});
dropElement.addEventListener("drop", (e) =>{
e.preventDefault();
if(e.dataTransfer.files.length){
inputElement.files= e.dataTransfer.files;
updateThumbnail(dropElement, e.dataTransfer.files[0]);
}
dropElement.classList.remove("dropfiles--over");
});
})
function updateThumbnail(dropElement, file){
console.log(dropElement);
console.log(file);
let ThumbElement = dropElement.querySelector(".thumb");
if (dropElement.querySelector(".prompt")){
dropElement.querySelector(".prompt").remove();
}
ThumbElement = document.createElement("div");
ThumbElement.classList.add("thumb")
dropElement.appendChild(ThumbElement);
ThumbElement.addEventListener("click", () => {
if(file.type.startsWith("image/")){
const content=document.getElementById("content");
content.classList.add("show");
reader.readAsDataURL(file)
reader.onload = () => {
content.style.backgroundImage = `url('${reader.result}')`;
}
}
})
if(file.type.startsWith("image/")){
const reader = new FileReader();
reader.readAsDataURL(file)
reader.onload = () => {
ThumbElement.style.backgroundImage = `url('${reader.result}')`
ThumbElement.dataset.label = file.name +" | Last Modified: "+file.lastModifiedDate;
}
}
if(file.type.startsWith("text/html")){
const reader = new FileReader();
reader.readAsDataURL(file)
reader.onload = () => {
ThumbElement.style.backgroundImage = `url('html.png')`
ThumbElement.dataset.label = file.name;
ThumbElement.style.width = '50%';
}
}
if(file.type.startsWith("application/x-javascript")){
ThumbElement.style.backgroundImage = `url('ohno.png')`
ThumbElement.dataset.label = "We do not accept Javascript files as they could be harmful.";
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="https://fonts.googleapis.com/css2?family=Material+Icons"
rel="stylesheet">
<title>Document</title>
<link rel="stylesheet" href="./file.css" />
<script src="https://kit.fontawesome.com/2b5f6cfacf.js" crossorigin="anonymous"></script>
</head>
<body>
<div class="dropfiles">
<div class="prompt">
<span class="prompt">Drop files in here, Or click to upload</span>
<input type="file" name="select" class="select"></input>
<div class="content" id="content"></div>
</div>
</div>
<script src="file.js"></script>
I have tried to do it this way with my code but then I get this error:
Uncaught TypeError: Cannot read properties of null (reading ‘classList’)
at HTMLDivElement.
I have checked to see if, maybe I didnt give it an ID, but I did, I checked if it hadn’t saved, and it did. I am not sure what to do, I have searched the seven seas (Stack Overflow) and none of those answers have helped me. SO pleeeeeez help meeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
