I'm trying to make a scroll view from scratch with 5 items on it and I'm using div tags for those items combined with jQuery and CSS classes.
The click event is being fired but for some reason the new class is not set (circle should be white) and it reverts to its default color.
I'm using Bootstrap 3 and latest jQuery
HTML
<div class="row-fluid">
<div class="col-md-1"></div>
<div class="col-md-2">
<div class="circle 1" id="circle_1>
</div>
</div>
<div class="col-md-2">
<div class="circle 2" id="circle_2">
</div>
</div>
<div class="col-md-2">
<div class="circle 3" id="circle_3">
</div>
</div>
<div class="col-md-2">
<div class="circle 4" id="circle_4">
</div>
</div>
<div class="col-md-2">
<div class="circle 5" id="circle_5">
</div>
</div>
<div class="col-md-1"></div>
</div>
CSS
/* CSS used here will be applied after bootstrap.css */
.circle-selected{
border-radius: 50%;
width: 100px;
height: 100px;
background: #ffffff;
cursor: pointer;
}
.circle {
border-radius: 50%;
width: 100px;
height: 100px;
background: #949494;
-webkit-transition: all ease .3s;
-moz-transition: all ease .3s;
-o-transition: all ease .3s;
transition: all ease .3s;
font-family: 'Corbel Bold';
/* width and height can be anything, as long as they're equal */
}
.circle:hover{
border-radius: 50%;
width: 100px;
height: 100px;
background: #ffffff;
cursor: pointer;
}
jQuery
$(".circle").click(function(){
var ID = $(this).attr('class').replace('circle ', '');
//alert('called 1');
$("#circle_" + ID).addClass('circle ' + ID).removeClass('circle-selected ' + ID);
});
$(".circle-selected").click(function(){
var ID = $(this).attr('class').replace('circle-selected ', '');
//alert('called 2');
$("#circle_" + ID).addClass('circle-selected ' + ID).removeClass('circle ' + ID);
});
Examples are the best so here's mine: http://www.bootply.com/6qrSq5KvkK
Can someone point it out what am I doing wrong here? I tried .switchClass and it doesn't work, so by searching SO I found an answer here jQuery UI switchClass() method is not working properly that suggest I should replace
.switchClass
With
addClass('square').removeClass('circle');