How can I activate multiple buttons on one page using JavaScript/jQuery?

Stiño

I want to build a page where a user can select multiple items by clicking on a button. I already used focus and active classes to make the buttons change appearance but with my current code I can only activate one single button. I'm looking for code that enables a user to activate multiple buttons.

HTML

<button class="btn btn-1">
    button 1
</button>

<button class="btn btn-1">
    button 2
</button>

<button class="btn btn-1">
    button 3
</button>

<button class="btn btn-1">
    button 4
</button>

CSS

.btn:hover {
  background-color: rgba(52, 152, 219,0.6);
}

.btn:focus {
  background-color: rgba(52, 152, 219,0.6);
}

.btn:active, .btn:visited {
  background-color: rgba(52, 152, 219,0.6);
}

.active {
  background-color: rgba(52, 152, 219,0.6); 
}

This is the jQuery code I have been using to try and add a permanent active class to those buttons that have been clicked. This is not working yet.

$(document).ready(function(){ 
    $('.btn').click(function() { 
        $('.btn').addClass('active');
    });
});
Bhushan Kawadkar

You should use $(this) to add active class to the clicked button

$(document).ready(function(){ 
    $('.btn').click(function() { 
       $(this).addClass('active');// use this here
    });
});

Demo

EDIT - as suggested by @Regent, you can use toggleClass to active and deactive on button click, use below code

$(document).ready(function(){ 
     $('.btn').click(function() { 
        $(this).toggleClass('active');
    });
 });

Demo for toggleClass Note - here I have commented .btn:focus css class because on second click button looks active as it is focused.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How can i save this multiple page scraping in to a csv with just one header and no index, with pandas

分類Dev

Using Rubberduck unit tests, how can I find out which one of multiple asserts failed?

分類Dev

How can I insert multiple records in mysql using php at once with one query dynamicaly?

分類Dev

How can I work with multiple values using ->?

分類Dev

How can I hide popovers on page when clicking a new one?

分類Dev

How can I do a form submit on ENTER when I have multiple possible submit buttons?

分類Dev

How can I copy multiple files over scp in one command?

分類Dev

how can i set the Tag value For multiple buttons in a single Cell in tableView in ios

分類Dev

How can I replace more than one string with another using just one Regex or do I have to use multiple Regex and temp strings?

分類Dev

MacOS: How can I activate fonts programmatically, systemwide?

分類Dev

How do I compress multiple files into one archive using lzma?

分類Dev

How can I calibrate my printer using the LaTeX test page?

分類Dev

How can I display select options as buttons?

分類Dev

How can I position the menu buttons in the center?

分類Dev

How can I align buttons on a toolbar?

分類Dev

how can I create a pyspark udf using multiple columns?

分類Dev

How can I add multiple inputs to this <div> using javascript function?

分類Dev

How can I upload multiple files using JavaScript and FastAPI?

分類Dev

How can I search users on Twitter using multiple filters?

分類Dev

How can I realize that android send one string to a php page and obtain a JSON array?

分類Dev

How can I make multiple on scroll fixed headers/navbars that stick at the very top of the page?

分類Dev

How can I get multiple files to upload to the server from a Javascript page without skipping?

分類Dev

How can I get a multiple page pdf output from php file?

分類Dev

How can i establish a one to one relationship between this two classes using spring boot jpa?, also, how do i save the entities?

分類Dev

How can I make a button redirect my page to another page using a controller action and passing a value using javascript?

分類Dev

How do I create a Rust HashMap where the value can be one of multiple types?

分類Dev

How can I pass multiple elements from a collection into a function with one or more of the elements being mutable?

分類Dev

How can I create multiple prepared statements from one mysql::Conn?

分類Dev

how can i execute multiple sql queries with node oracledb plugin in one session

Related 関連記事

  1. 1

    How can i save this multiple page scraping in to a csv with just one header and no index, with pandas

  2. 2

    Using Rubberduck unit tests, how can I find out which one of multiple asserts failed?

  3. 3

    How can I insert multiple records in mysql using php at once with one query dynamicaly?

  4. 4

    How can I work with multiple values using ->?

  5. 5

    How can I hide popovers on page when clicking a new one?

  6. 6

    How can I do a form submit on ENTER when I have multiple possible submit buttons?

  7. 7

    How can I copy multiple files over scp in one command?

  8. 8

    how can i set the Tag value For multiple buttons in a single Cell in tableView in ios

  9. 9

    How can I replace more than one string with another using just one Regex or do I have to use multiple Regex and temp strings?

  10. 10

    MacOS: How can I activate fonts programmatically, systemwide?

  11. 11

    How do I compress multiple files into one archive using lzma?

  12. 12

    How can I calibrate my printer using the LaTeX test page?

  13. 13

    How can I display select options as buttons?

  14. 14

    How can I position the menu buttons in the center?

  15. 15

    How can I align buttons on a toolbar?

  16. 16

    how can I create a pyspark udf using multiple columns?

  17. 17

    How can I add multiple inputs to this <div> using javascript function?

  18. 18

    How can I upload multiple files using JavaScript and FastAPI?

  19. 19

    How can I search users on Twitter using multiple filters?

  20. 20

    How can I realize that android send one string to a php page and obtain a JSON array?

  21. 21

    How can I make multiple on scroll fixed headers/navbars that stick at the very top of the page?

  22. 22

    How can I get multiple files to upload to the server from a Javascript page without skipping?

  23. 23

    How can I get a multiple page pdf output from php file?

  24. 24

    How can i establish a one to one relationship between this two classes using spring boot jpa?, also, how do i save the entities?

  25. 25

    How can I make a button redirect my page to another page using a controller action and passing a value using javascript?

  26. 26

    How do I create a Rust HashMap where the value can be one of multiple types?

  27. 27

    How can I pass multiple elements from a collection into a function with one or more of the elements being mutable?

  28. 28

    How can I create multiple prepared statements from one mysql::Conn?

  29. 29

    how can i execute multiple sql queries with node oracledb plugin in one session

ホットタグ

アーカイブ