I'm making a quiz using jquery and would like to be able to change the color of all the button elements in a question class relatively. Many examples I've seen show how to do it in an absolute manner--meaning all the elements of all instances of a class change color. How do you iterate through a single instance of div class?
function clickFn() {
console.log('in click event')
$( "button" ).each(function() {
if ($(this)
$(this).toggleClass( "buttonSelected" );
});
};
.button {
background-color: grey;
border: none;
color: white;
width: 20vw;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
.buttonSelected {
background-color: #f00;
border: none;
color: white;
width: 20vw;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='question'>
<span class='question_body' onclick="clickFn()">Question n </span></p>
<button class="button" onclick="clickFn()">Wrong Answer</button></p>
<button class="button" onclick="clickFn()">Correct Answer</button></p>
<button class="button" onclick="clickFn()">Wrong Answer</button></p>
<button class="button" onclick="clickFn()">Wrong Answer</button></p>
</div>
<div id='question'>
<span class='question_body'>Question n </span></p>
<button class="button">Wrong Answer</button></p>
<button class="button">Correct Answer</button></p>
<button class="button">Wrong Answer</button></p>
<button class="button">Wrong Answer</button></p>
</div>
This will change the color