I work primarily with PHP and don’t understand JavaScript very well. I’m working with a multi-choice quiz script that has some JavaScript functions. it works fine, but it’s very hard to add classes or ID’s to any elements without killing the function.
For example, the HTML begins with this…
<div id="quiz" rel="key">
<ol>
I’d like to change it to this…
<div id="quiz" rel="key">
<ol class="Answers">
But when I do that the quiz engine no longer works. I suspect I’m going to have a lot of trouble using jQuery to add and delete classes when people push the submit button.
I posted some code at http://jsfiddle.net/5q2xoLn7/4/ However, it doesn’t actually work because I don’t know how to incorporate the JSON answer key, which is stored in a separate file.
Does anyone have any general tips for fixing this kind of problem, or do I just have to learn JavaScript well enough to go through the code line by line, modifying it to accept every change in a class or ID?
You’re right; I chose a bad example. But it is a pain when I can’t add or change a class or ID, or I can’t even add a second class without killing the function. The code below was working perfectly with my first quiz script. At first, when I click on an answer, it was highlighted. If I changed my mind and chose a second answer, BOTH answers were highlighted. The code below fixes that so that when you click on a second answer, the first choice is “deselected.”
But when I tried a new quiz script, I ran into problems. The code still assigns the class “selected” to answers I click, but it no longer deselects answers when I click additional answers. I changed this.id to this.class to match my new script. But it didn’t deselect answers, plus it created some kind of conflict that killed the script’s function. So I inserted a div inside each li tag, and modified the script to select .Question li div.
Now the script works pretty good. The quiz actually works, and answers are highlighted when you click on them. But, again, the deselect (removeClass) function doesn’t work.
This is what my HTML looks like:
<div id="quiz" rel="key">
<ol>
<li id="q1" class="Question"><span class="Q2">1. No one knows if there’s just one universe or a series of universes, sometimes referred to as a:</span>
<ol>
<li class="1-A">
<div class="Ans2">
<label> A.
<input type="radio" name="q1">
multiverse </label>
</div>
</li>
<li class="1-B">
<div class="Ans2">
<label> B.
<input type="radio" name="q1">
parallel universe </label>
</div>
</li>
</ol>
</li>
<li id="q2" class="Question"><span class="Q2">2. Scientists believe the universe is:</span>
<ol>
<li class="2-A">
<div class="Ans2">
<label> A.
<input type="radio" name="q2">
shrinking </label>
</div>
</li>
<li class="2-B">
<div class="Ans2">
<label> B.
<input type="radio" name="q2">
expanding </label>
</div>
</li>
</ol>
</li>
</ol>
<input type="button" value="Submit" class="submit">
<p class="feedback"></p>
</div>
I encountered one more problem…when I moved on to Question #2 and make a selection, it removed all the “selected” classes from Question #1. However, I was able to fix that by simply changing parents(‘ol’) to parents(‘ol ol’), better reflecting the structure of my HTML.