最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I activate multiple buttons on one page using JavaScriptjQuery? - Stack Overflow

programmeradmin1浏览0评论

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');
    });
});

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');
    });
});
Share Improve this question edited Sep 12, 2014 at 12:57 Regent 5,1763 gold badges23 silver badges35 bronze badges asked Sep 12, 2014 at 12:42 StiñoStiño 2,8637 gold badges29 silver badges47 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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 mented .btn:focus css class because on second click button looks active as it is focused.

This should do it.

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

I think if you use "this" the action only takes effect in the button you are clicking itself.

发布评论

评论列表(0)

  1. 暂无评论