The Prologue & the ask: I’ve been struggling with this problem for the past day, and now I fear I might be in too deep to see a diff strategy; and politely & humbly ask for a multitude of fresh set of eyes to assist me with this issue please. (together, we are amazing.)
=).
The problem: Alright, so I’m going to attach some CSS, HTML, and JavaScript, after asking my question that I seek assistance with. That question sounds like this:
A.) I have a ‘Would you like to upload an Audio?’ button.
B.) I have a text box area thing (called: “messageBox”) where ppl can write stuff like: “I love what I do.” and press submit.
And when they press submit? it shows up in a new area,
in a container called: “BottomRightContainer”.
C.) So, i have a button called:
“Would you like to upload an Audio” button, that (i referenced earlier and)
has the ability to upload multiple audio files (cool).
when we click it, and add an audio (MP3) file?
it gets added to our “messageBox”. (Cool. this is desired.
When we click play? while this MP3 audio file is in the messagebox? it plays. cool. This is desired behavior. everything’s groovy so far homies).
D.1) Now here’s where our issue shows up,
it shows up with an uploaded audio file.
When we upload an audio file and then: “When we click submit?”
the audio file goes from “messageBox” to
“BottomRightContainer”. cool. this is wanted and desired behavior.
D.2) But, for some reason, there’s 2 of em. (this, is NOT, desired behavior).
D.3) and, of the two, one of em does not work. (This is where I need help, and humbly ask for help, REMOVING THE ONE- THAT DOES NOT WORK (AND KEEPING THE ONE THAT DOES- PLEASE)).
D.3.1) and of the two, I seem to have the magical ability, to keep deleting the one that works. Even when I delete different parts of the code.
Here is my CSS HTML and Javascript, all copy paste friendly for one page:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
/* Fancy MP3 info id like to integrate, found below: */
.inline-audio {
display: inline-block;
vertical-align: middle;
}
/* MP3 Player Styling */
.audio-player {
display: flex;
align-items: center;
gap: 10px;
margin: 10px 0;
padding: 10px;
border: 1px solid #ccc;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
background-color: #333;
color: #fff;
}
.audio-player button {
/* MP3 Play Button */
background-color: #007bff;
color: white;
border: none;
border-radius: 50%;
width: 40px;
height: 40px;
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
}
.audio-player button:hover {
/* Play Button Hover */
background-color: #0056b3;
}
.audio-player .progress {
/* Progress Bar */
flex: 1;
height: 5px;
background-color: #ddd;
border-radius: 5px;
overflow: hidden;
}
.audio-player .progress-bar {
/* Progress Bar Fill */
height: 100%;
background-color: #007bff;
width: 0;
}
.audio-title {
/* MP3 Title */
flex: 2;
}
.audio-duration {
/* MP3 Duration */
flex: 1;
text-align: right;
}
/* Background info, is the CSS found below */
.toolbar {
display: flex;
justify-content: flex-start;
white-space: normal;
margin-bottom: 10px;
}
.toolbar button,
.toolbar select,
.toolbar input {
margin-right: 5px;
padding: 5px 10px;
background: linear-gradient(225deg, #e6e, #ffeb3b); /*As a Thank You, for looking through my Code, i give you, these lovely colors- that i call: "YeBoyPurple" */
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
button {
padding: 10px 15px;
border: none;
border-radius: 5px;
cursor: pointer;
background: linear-gradient(225deg, #e6e, #ffeb3b);
color: #fff;
font-size: 16px;
}
#messageBox {
width: 97%;
height: 100px;
padding: 10px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 5px;
resize: none;
margin-bottom: 10px;
background-color: #fff;
opacity: 1;
overflow-y: auto;
display: flex;
flex-direction: column;
justify-content: space-between;
}
</style>
</head>
<body>
<div id="BottomContainer">
<div
id="BottomContainerHoldingImageAndMessage"
style="display: flex; flex-direction: row"
>
<div id="BottomLeftContainer">
<div class="toolbar">
<button data-command="insertAudio">Audio?</button>
</div>
<div contenteditable="true" id="messageBox"></div>
<div
class="flexContainer"
style="display: flex; justify-content: space-between"
>
<div><button id="submitButton">Submit</button></div>
<div id="buttonContainer"></div>
</div>
</div>
</div>
</div>
<div id="BottomRightContainer">
<!-- Messages will appear here- like this one: "This Code was Written By: Scholah on StackOverflow" <3 -->
</div>
<script>
const messageBox = document.getElementById("messageBox");
const submitButton = document.getElementById("submitButton");
const bottomRightContainer = document.getElementById(
"BottomRightContainer",
);
const bottomLeftContainer = document.getElementById(
"BottomLeftContainer",
);
const modeSwitch = document.getElementById("modeSwitch");
let audioFiles = [];
document.querySelectorAll("[data-command]").forEach((button) => {
button.addEventListener("click", () => {
const command = button.getAttribute("data-command");
if (
command === "bold" ||
command === "italic" ||
command === "underline"
) {
document.execCommand(command, false, null);
} else if (command === "insertAudio") {
const input = document.createElement("input");
input.type = "file";
input.accept = "audio/*";
input.multiple = true;
input.onchange = function (event) {
const files = Array.from(event.target.files); // Get the selected audio files
files.forEach((file) => {
const audioPlayer = document.createElement("div");
audioPlayer.className = "audio-player"; // Apply Background MP3 styling
const playButton = document.createElement("button");
playButton.innerHTML = "▶"; // Play button
audioPlayer.appendChild(playButton);
const audio = document.createElement("audio");
audio.src = URL.createObjectURL(file); // Set the audio source to the uploaded file
const progress = document.createElement("div");
progress.className = "progress"; // Progress bar container
const progressBar = document.createElement("div");
progressBar.className = "progress-bar"; // Progress bar fill
progress.appendChild(progressBar);
audioPlayer.appendChild(progress);
const title = document.createElement("span");
title.className = "audio-title"; // Display the MP3 file name
title.textContent = file.name;
audioPlayer.appendChild(title);
const duration = document.createElement("span");
duration.className = "audio-duration"; // Display the audio duration
audio.addEventListener("loadedmetadata", () => {
duration.textContent = formatTime(audio.duration);
});
audioPlayer.appendChild(duration);
audio.addEventListener("timeupdate", () => {
// Update the progress bar as the audio plays
const percentage = (audio.currentTime / audio.duration) * 100;
progressBar.style.width = `${percentage}%`;
});
playButton.addEventListener("click", () => {
// Toggle play/pause functionality
if (audio.paused) {
audio.play();
playButton.innerHTML = "❚❚"; // Change to pause button
} else {
audio.pause();
playButton.innerHTML = "▶"; // Change to play button
}
});
messageBox.appendChild(audioPlayer); // Add custom MP3 player to messageBox
});
};
input.click();
}
});
});
// Handle the submit button click without reattaching event listeners
submitButton.addEventListener("click", () => {
const messageContent = messageBox.innerHTML;
if (messageContent) {
const messageDiv = document.createElement("div");
messageDiv.innerHTML = `<p>${messageContent}</p>`;
// Move existing audio players from the messageBox
const audioPlayers = messageBox.querySelectorAll(".audio-player");
audioPlayers.forEach((audioPlayer) => {
messageDiv.appendChild(audioPlayer); // Move audio player to messageDiv
});
bottomRightContainer.insertBefore(
messageDiv,
bottomRightContainer.firstChild,
);
bottomRightContainer.style.display = "block";
messageBox.innerHTML = "";
audioFiles = [];
document.execCommand("removeFormat", false, null);
}
});
messageBox.contentEditable = true;
messageBox.spellcheck = true;
</script>
</body>
</html>
Conclusion: So i come to StackOverflow, to ask for help.
Here’s what i’ve tried and where im at, but i do not recommend reading this next part, because it might taint your attempt at trying to help me, by implementing in your minds eye, a potential wrong path, cause again, im on the wrong end here. anywho, what:
I’ve tried for roughly a day to get to this area. for a hot minute it would not show up in its proper colors. idk why but it didnt. then when i finally got it to show up. it didnt play. then i kept at it till now i somehow managed to come to an understanding that: ‘the reason it didnt play, is because its connected to the DOM(?), and when it gets transferred over, all the event listeners are NOT getting transferred over(?) so i had to duplicate the event listeners(?) and implement them in(?)
which, makes me think: “in its duplication” it might be duplicating the audio file to show up twice? but somehow, the i think DOM duplication thing also worked(?) cause one of the two work? so, like, now i tried removing the first one(?) but then that removes the duplicate(?)so then i tried hiding the first one(?) but then that hid the duplicate(?) and a part of me is like: “why is this taking a whole day? am i in too deep? am i overthinking this? This shouldnt be taking a whole day and im not gonna go into this for two days without some extra help. thas my motto yo. “you get one day. two days? i come back with StackOverflow reinforcements.” If able to figure out how to help me: “move the audio from one box to the other while keeping how it looks and its style and playing without there being a duplicate” that’d be greatly appreciative and id be thankful. Thank you for the priviledge of your time. If unable to help but read all this anywho, thank you for reading =).
(Extra Note:> i also, very very very polietely ask, the stackoverflow editors, to please stop editing out my slang and linguistic flow. we are all flowers of all different varieties, and to grow our coding/programming field, i politely ask the editors, to stop gatekeeping/removing the flavor of our (and my) verbal flow (regardless of your reasoning- thanks) as i ask my question in how i ask it, and then can properly follow up with people who try to help, in a consistent verbal flow- rather than sound one way and then a whole other way since the editing made me sound more like not me due to your lovely edits <3 please and thank you.)
(Extra Extra Note: comments to help me improve the craft (not my linguistic flow) and ideas on how to better tackle on such ideas- are always welcomed and greatly desired/preferred/ & appreciated! THANK YOU!!)
(Extra Extra Extra Note: Teachers, feel free, taking this as a lesson (especially if we find the solution) and tailoring it your own way =) )
HAVE A GREAT DAY EVERYBODY!!!