Reputation: 1779
I am trying to change the class of a button when clicking on it. Why isn't mine working: see my fiddle
HTML:
<button id="repeatMon" onclick="changeStyle()"><span> Mon </span></button>
<br/>
<button id="repeatTues" data-bind="checked: repeatTues" onclick="changeStyle()"><span> Tues </span></button>
<br/>
<button id="repeatWeds" data-bind="checked: repeatWeds" onclick="changeStyle()"><span> Weds </span></button>
<br/>
<button id="repeatThurs" data-bind="checked: repeatThurs" onclick="changeStyle()"><span> Thurs </span></button>
<br/>
<button id="repeatFri" data-bind="checked: repeatFri" onclick="changeStyle()"><span> Fri </span></button>
<br/>
<button id="repeatSat" data-bind="checked: repeatSat" onclick="changeStyle()"><span> Sat </span></button>
<br/>
<button id="repeatSun" data-bind="checked: repeatSun" onclick="changeStyle()"><span> Sun </span></button>
<br/>
CSS:
button{
width: 60px;
height: 35px;
border-radius: 0px;
color: #cfcfcf;
background: #0a4a58;
}
.active{
color: #fff;
background: ##02AEFF;
}
jQuery:
function changeStyle(){
$(this).toggleClass('active');
}
thank you!
Upvotes: 0
Views: 192
Reputation: 318182
You're using jQuery, remove the inline JS and do it the proper way :
$('[id^="repeat"]').on('click', function() {
$(this).toggleClass('active');
});
To toggle the active state on one button at the time :
$('[id^="repeat"]').on('click', function() {
$(this).toggleClass('active').siblings().removeClass('active');
});
Upvotes: 5
Reputation: 103358
<button id="repeatSun" data-bind="checked: repeatSun" onclick="changeStyle(this)"><span> Sun </span></button>
function changeStyle(btn){
$(btn).toggleClass('active');
}
Alternatively a better method would be to add click event handlers with your jQuery.
Add the same class to all your buttons and apply a click event handler to that class:
<button id="repeatSun" data-bind="checked: repeatSun" class="changestyle"><span> Sun </span></button>
$(".changestyle").click(function(){
$(".changestyle").removeClass('active');
$(this).addClass('active');
});
Upvotes: 4
Reputation: 4901
What about something like this:
$("button").click(function () {
$(this).toggleClass('active');
});
EDIT: Only one button at a time.
$("button").click(function () {
$( "button" ).each(function() {
$(this).removeClass("active");
});
$(this).toggleClass('active');
});
Upvotes: 1
Reputation: 4653
You are using jquery so you don't have to repeat yourself. Remove onclick
from button and do it with jquery
<button id="repeatMon"><span> Mon </span></button> <br/>
<button id="repeatTues" data-bind="checked: repeatTues"><span> Tues </span></button><br/>
<button id="repeatWeds" data-bind="checked: repeatWeds"><span> Weds </span></button><br/>
<button id="repeatThurs" data-bind="checked: repeatThurs"><span> Thurs </span></button><br/>
<button id="repeatFri" data-bind="checked: repeatFri"><span> Fri </span></button><br/>
<button id="repeatSat" data-bind="checked: repeatSat"><span> Sat </span></button><br/>
<button id="repeatSun" data-bind="checked: repeatSun"><span> Sun </span></button><br/>
remove the extra hash (#) from your color in css in class active
.active {
color: #fff;
background: #02AEFF;
}
and write the script
$(function() {
var buttons = $('button[id^="repeat"]');
buttons.click(function() {
buttons.removeClass('active');
$(this).addClass('active');
});
});
Upvotes: 2
Reputation: 35973
try this:
<button id="repeatMon" class="clickable"><span> Mon </span></button>
Jquery inside document.ready()
$('.clickable').click(function(){
$(this).toggleClass('active');
});
Upvotes: 1
Reputation: 167172
You need to pass the button.
function changeStyle(e){
$(e).toggleClass('active');
}
And in the HTML:
<button id="repeatSun" data-bind="checked: repeatSun" onclick="changeStyle(this)"><span> Sun </span></button>
Upvotes: 1