How do you interact with background button?
-
There's a group of buttons. I need the button to change the color of the button and remain so until the user presses another button.
Now I tried to do it with mousedown and mouseup, but it didn't help. The color of the button changes for a few seconds and returns to the old state.
$(function() { $('.btn').on('mousedown', function() { $(this).addClass('down'); }); $('.btn').on('mouseup', function() { $(this).removeClass('down'); }); });
.down { background: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> выберите что-то одно <div class="btns"> <button class="btn">Тест1</button> <button class="btn">тест 2</button> <button class="btn">тест 3</button> <button class="btn">тест 4</button> </div>
-
$(function() { $('.btns .btn').on('click', function() { $(this).closest('.btns').find('.btn.down').removeClass('down'); $(this).addClass('down'); }); });
.down { background: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> выберите что-то одно <div class="btns"> <button class="btn">Тест1</button> <button class="btn">тест 2</button> <button class="btn">тест 3</button> <button class="btn">тест 4</button> </div>