- Joined
- Feb 3, 2013
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
} else {
generatedQuiz[currentQuestion].answers.forEach(addButton)
function addButton(thisAnswer) {
let answerButton = document.createElement("button")
answerButton.textContent = thisAnswer.text
answerButton.style.fontFamily = "Corbel"
answerButton.style.padding = "1em 2em 1em 2em"
answerButton.className = "option"
answerContainer.appendChild(answerButton)
answerButton.addEventListener("click", buttonClickResponse)
function buttonClickResponse() {
if (thisAnswer.isCorrect) {
console.log("I guess you're right")
clearInterval(questionCountdown)
correctAnswerCount++
nextButton.addEventListener("click", showNextQuestion)
} else {
console.log("u wrong")
clearInterval(questionCountdown)
nextButton.addEventListener("click", showNextQuestion)
}
answerButton.removeEventListener("click", buttonClickResponse)
}
}
}
As is often the case in vanilla JS, it's possible but convoluted. See the "Using an abort signal" section near the bottom of this page. Check out this browser compatibility table if you're worried about older browsers.Okay i'm almost finished this but I'm stuck on one thing.
Is there a way in vanilla Javascript to remove multiple event listeners in this case? I used a forEach method to append a button along with an eventListener to each item in an array, and want to make it so that if you click one button, eventListeners for all buttons immediately go away (i.e. all buttons are disabled).
I tried making buttonClickResponse() disable itself by either setting it equal to "null" or redefining it as an empty func, but I got nothing. Also checked multiple stackOverflows and got nothing as well. And of course, the "removeEventListener" thing at the end of buttonClickResponse() doesn't work; I'm just using it as a placeholder.
JavaScript:} else { generatedQuiz[currentQuestion].answers.forEach(addButton) function addButton(thisAnswer) { let answerButton = document.createElement("button") answerButton.textContent = thisAnswer.text answerButton.style.fontFamily = "Corbel" answerButton.style.padding = "1em 2em 1em 2em" answerButton.className = "option" answerContainer.appendChild(answerButton) answerButton.addEventListener("click", buttonClickResponse) function buttonClickResponse() { if (thisAnswer.isCorrect) { console.log("I guess you're right") clearInterval(questionCountdown) correctAnswerCount++ nextButton.addEventListener("click", showNextQuestion) } else { console.log("u wrong") clearInterval(questionCountdown) nextButton.addEventListener("click", showNextQuestion) } answerButton.removeEventListener("click", buttonClickResponse) } } }
It's been a while since I did JavaScript, but isn't this something you could elegantly handle by just having some extra state kicking around? E.g. a booleanOkay i'm almost finished this but I'm stuck on one thing.
Is there a way in vanilla Javascript to remove multiple event listeners in this case? I used a forEach method to append a button along with an eventListener to each item in an array, and want to make it so that if you click one button, eventListeners for all buttons immediately go away (i.e. all buttons are disabled).
I tried making buttonClickResponse() disable itself by either setting it equal to "null" or redefining it as an empty func, but I got nothing. Also checked multiple stackOverflows and got nothing as well. And of course, the "removeEventListener" thing at the end of buttonClickResponse() doesn't work; I'm just using it as a placeholder.
JavaScript:} else { generatedQuiz[currentQuestion].answers.forEach(addButton) function addButton(thisAnswer) { let answerButton = document.createElement("button") answerButton.textContent = thisAnswer.text answerButton.style.fontFamily = "Corbel" answerButton.style.padding = "1em 2em 1em 2em" answerButton.className = "option" answerContainer.appendChild(answerButton) answerButton.addEventListener("click", buttonClickResponse) function buttonClickResponse() { if (thisAnswer.isCorrect) { console.log("I guess you're right") clearInterval(questionCountdown) correctAnswerCount++ nextButton.addEventListener("click", showNextQuestion) } else { console.log("u wrong") clearInterval(questionCountdown) nextButton.addEventListener("click", showNextQuestion) } answerButton.removeEventListener("click", buttonClickResponse) } } }
questionAnswered
that you set equal to true
when a button has been clicked, and then you just update buttonClickResponse()
to test whether questionAnswered
is false
before you do anything else in that function? I guess I don't see the point of dynamically destroying callbacks when you can just render them inert instead.Does anyone have a preferred resource for learning Java and Python? Preferably free, and with practice projects? I want to Learn to Code™ and don’t really know where to start. I’m very proficient with MATLAB so I’m not starting from scratch here, but proper programming languages are still kinda intimidating.
Okay i'm almost finished this but I'm stuck on one thing.
generatedQuiz[currentQuestion].answers.forEach(addButton)
function addButton(thisAnswer) {
let answerButton = document.createElement("button")
answerButton.textContent = thisAnswer.text
answerButton.style.fontFamily = "Corbel"
answerButton.style.padding = "1em 2em 1em 2em"
answerButton.id = "option"
answerContainer.appendChild(answerButton)
answerButton.addEventListener("click", buttonClickResponse)
function buttonClickResponse() {
if (thisAnswer.isCorrect) {
console.log("I guess you're right")
clearInterval(questionCountdown)
correctAnswerCount++
nextButton.addEventListener("click", showNextQuestion)
} else {
console.log("u wrong")
clearInterval(questionCountdown)
nextButton.addEventListener("click", showNextQuestion)
}
generatedQuiz[currentQuestion].answers.forEach(replaceButton)
function replaceButton(thisAnswer) {
document.getElementById("option").remove()
let emptyButton = document.createElement("button")
emptyButton.textContent = thisAnswer.text
emptyButton.style.fontFamily = "Corbel"
emptyButton.style.padding = "1em 2em 1em 2em"
emptyButton.className = "option"
answerContainer.appendChild(emptyButton)
}
}
}
Does anyone have a preferred resource for learning Java and Python? Preferably free, and with practice projects? I want to Learn to Code™ and don’t really know where to start. I’m very proficient with MATLAB so I’m not starting from scratch here, but proper programming languages are still kinda intimidating.
Learn X in Y is a pretty good resource for starting to learn most programming languages, algorithms, data structures, and tools like Git in multiple spoken languages. Which is a fun way to teach yourself a foreign language imho. Each article on a langue will link you to the language resource code, give you an inline and commented example like cheat sheet, and links to additional resources like books, websites, sometimes tools at the end. Check out their Python, Java, or even the MATLAB(as a judgement gauge) articles to see if it appeals to your needs.Does anyone have a preferred resource for learning Java and Python? Preferably free, and with practice projects? I want to Learn to Code™ and don’t really know where to start. I’m very proficient with MATLAB so I’m not starting from scratch here, but proper programming languages are still kinda intimidating.
Is your goal to physically disable the buttons? or prevent them from doing anything?Solved it. Made "buttonClickResponse" go through the array again in order to replace all existing answer buttons with empty buttons that have no eventListeners attached.
JavaScript:generatedQuiz[currentQuestion].answers.forEach(addButton) function addButton(thisAnswer) { let answerButton = document.createElement("button") answerButton.textContent = thisAnswer.text answerButton.style.fontFamily = "Corbel" answerButton.style.padding = "1em 2em 1em 2em" answerButton.id = "option" answerContainer.appendChild(answerButton) answerButton.addEventListener("click", buttonClickResponse) function buttonClickResponse() { if (thisAnswer.isCorrect) { console.log("I guess you're right") clearInterval(questionCountdown) correctAnswerCount++ nextButton.addEventListener("click", showNextQuestion) } else { console.log("u wrong") clearInterval(questionCountdown) nextButton.addEventListener("click", showNextQuestion) } generatedQuiz[currentQuestion].answers.forEach(replaceButton) function replaceButton(thisAnswer) { document.getElementById("option").remove() let emptyButton = document.createElement("button") emptyButton.textContent = thisAnswer.text emptyButton.style.fontFamily = "Corbel" emptyButton.style.padding = "1em 2em 1em 2em" emptyButton.className = "option" answerContainer.appendChild(emptyButton) } } }
Is your goal to physically disable the buttons? or prevent them from doing anything?
instead of removing and replacing the buttons, you should be able to do this:I am currently making a quiz application which includes multiple choice questions.
For the multiple choice questions, I used a forEach loop to add a button and an eventListener to each answer, which is contained in an array. Basically, what I wanted to do was make the click of one button disable all buttons' functionalities, including the one just clicked.
My goal was to remove all eventListeners upon one click on any of the buttons. So I basically accessed the existing buttons by calling its HTML id (i.e. "option") and removed them, then replaced those buttons with empty versions of those buttons that have no eventListeners, which are basically there to retain the appearance of question even though the eventListeners have been taken out
document.getElementById("option").onclick = function(event) { event.preventDefault(); }
It looks like you've already solved this, but: yes, each event listener that you add can be removed, if you remove exactly the same function object that you added.Okay i'm almost finished this but I'm stuck on one thing.
Is there a way in vanilla Javascript to remove multiple event listeners in this case? I used a forEach method to append a button along with an eventListener to each item in an array, and want to make it so that if you click one button, eventListeners for all buttons immediately go away (i.e. all buttons are disabled).
I tried making buttonClickResponse() disable itself by either setting it equal to "null" or redefining it as an empty func, but I got nothing. Also checked multiple stackOverflows and got nothing as well. And of course, the "removeEventListener" thing at the end of buttonClickResponse() doesn't work; I'm just using it as a placeholder.
JavaScript:} else { generatedQuiz[currentQuestion].answers.forEach(addButton) function addButton(thisAnswer) { let answerButton = document.createElement("button") answerButton.textContent = thisAnswer.text answerButton.style.fontFamily = "Corbel" answerButton.style.padding = "1em 2em 1em 2em" answerButton.className = "option" answerContainer.appendChild(answerButton) answerButton.addEventListener("click", buttonClickResponse) function buttonClickResponse() { if (thisAnswer.isCorrect) { console.log("I guess you're right") clearInterval(questionCountdown) correctAnswerCount++ nextButton.addEventListener("click", showNextQuestion) } else { console.log("u wrong") clearInterval(questionCountdown) nextButton.addEventListener("click", showNextQuestion) } answerButton.removeEventListener("click", buttonClickResponse) } } }
function buttonClickResponse()
inside of the forEach
(which is a new scope), it's being defined each time the forEach
executes. And that could make it hard to track -- unless you're pushing them into an array that's outside that scope, or something, so that you can remove all of them.forEach
and into a broader scope, so it's only defined once, could probably help in situations like this.Python Crash Course is a good start for Python and free. It's worth a buy if it works for you,Does anyone have a preferred resource for learning Java and Python? Preferably free, and with practice projects? I want to Learn to Code™ and don’t really know where to start. I’m very proficient with MATLAB so I’m not starting from scratch here, but proper programming languages are still kinda intimidating.
Modern day SICP appears to be taught with Python nowadays, as opposed to MIT Scheme.Does anyone have a preferred resource for learning Java and Python? Preferably free, and with practice projects? I want to Learn to Code™ and don’t really know where to start. I’m very proficient with MATLAB so I’m not starting from scratch here, but proper programming languages are still kinda intimidating.
Zed Shaw's books are good if you're willing to spend less time reading and more time solving problems. His teaching style is, "here's the barebones of what you need, now you figure out how to solve these problems (which will also give you the right hints to infer the most critical knowledge)"Has anyone read any of Zed Shaw's books like Learn Python The Hard Way? Are they worth it? I know he has an internet tough guy persona that's a little cringe there are a lot of people recommending his books.
The Python tutorial on Python.org gives a good overview of the language. It might not be the best resource for learning to code but I'd specifically highly recommend reading it's chapter on classes and namespaces. Namespaces are something that always trip up new programmers and this article gives a really thorough explanation of how they work in PythonDoes anyone have a preferred resource for learning Java and Python? Preferably free, and with practice projects? I want to Learn to Code™ and don’t really know where to start. I’m very proficient with MATLAB so I’m not starting from scratch here, but proper programming languages are still kinda intimidating.
In JS you should be able to use removeEventListener method of each DOM element, but you need to have the function you are removing.I am currently making a quiz application which includes multiple choice questions.
For the multiple choice questions, I used a forEach loop to add a button and an eventListener to each answer, which is contained in an array. Basically, what I wanted to do was make the click of one button disable all buttons' functionalities, including the one just clicked.
My goal was to remove all eventListeners upon one click on any of the buttons. So I basically accessed the existing buttons by calling its HTML id (i.e. "option") and removed them, then replaced those buttons with empty versions of those buttons that have no eventListeners, which are basically there to retain the appearance of question even though the eventListeners have been taken out
no it sucks. has bad pedagogy and makes asinine statements.Has anyone read any of Zed Shaw's books like Learn Python The Hard Way? Are they worth it? I know he has an internet tough guy persona that's a little cringe there are a lot of people recommending his books.
git init && git submodule --init --recursive
[submodule "win32/libpng/src"]
path = win32/libpng/src
url = https://git.code.sf.net/p/libpng/code
[submodule "win32/zlib/src"]
path = win32/zlib/src
url = https://github.com/madler/zlib.git
[submodule "win32/DirectXMath"]
path = win32/DirectXMath
url = https://github.com/Microsoft/DirectXMath
[submodule "shaders/SPIRV-Cross"]
path = shaders/SPIRV-Cross
url = https://github.com/KhronosGroup/SPIRV-Cross.git
[submodule "shaders/glslang"]
path = shaders/glslang
url = https://github.com/KhronosGroup/glslang.git
[submodule "vulkan/vulkan"]
path = vulkan/vulkan
url = https://github.com/KhronosGroup/Vulkan-Headers.git