I have a problem. Two buttons with class .sert_btn should toggle classes of images. The left button works in a right way, but the rigth one doesn’t. HELP PLS
class passive – display: none;
I tried another constructions for $(document), as $(‘.sertbtn) and $(‘#btn_r’), but result is the same
let i = 0;
$(document).on('click', '.sert_btn', function() {
i++;
if (i % 2 == 0) {
$('#s_1').addClass('passive');
$('#s_2').removeClass('passive');
} else {
$('#s_2').addClass('passive');
$('#s_1').removeClass('passive');
}
});
.passive { display: none }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
<div class="docs docs_dt">
<button class="sert_btn">Click</button>
<img id="s_1" class="sert_img" src="https://pngfre.com/wp-content/uploads/Turtle-1-300x187.png">
<img id="s_2" class="sert_img" src="https://pngfre.com/wp-content/uploads/Penguin-png-1-155x300.png">
<button id="btn_r" class="sert_btn">Click</button>
</div>
2
Answers
This is simpler and makes more sense
This code should toggle the class on the images each time you click either of the two buttons.