I'm just learning JavaScript and jQuery, and I've got some code that works, but I know it can be optimized. I have a page that when a button is clicked it adds 1 to a label, and then increments a bootstrap progress bar by 1. I have 6 functions that could probably be reduced to 2 by passing in arguments instead of having 6 hard coded functions.
Would it be possible to reduce these 6 down to just 1? Also, am I using the if
/else
blocks right? Or is there a better way to do that?
Again, I am just learning, but I want to learn the right techniques and formats.
$(document).ready(function(){
$("#button1").click(addVisit);
$("#button2").click(removeVisit);
$("#button3").click(addVisit2);
$("#button4").click(removeVisit2);
$("#button5").click(addVisit3);
$("#button6").click(removeVisit3);
});
function addVisit(){
var countTag = $('#count');
var count = countTag.text();
var progressTag = $('#percent');
count ++;
if (count <= 4){
progressTag.removeClass('progress-bar-warning');
progressTag.removeClass('progress-bar-danger');
progressTag.addClass('progress-bar-success');
} else if (count > 4 && count < 8){
progressTag.removeClass('progress-bar-success');
progressTag.removeClass('progress-bar-danger');
progressTag.addClass('progress-bar-warning');
} else if (count >= 8){
progressTag.removeClass('progress-bar-warning');
progressTag.removeClass('progress-bar-success');
progressTag.addClass('progress-bar-danger');
}
countTag.text(count);
progressTag.css('width', count * 10 + "%");
}
function removeVisit(){
var countTag = $('#count');
var count = countTag.text();
var progressTag = $('#percent');
count --;
if (count <= 4){
progressTag.removeClass('progress-bar-warning');
progressTag.removeClass('progress-bar-danger');
progressTag.addClass('progress-bar-success');
} else if (count > 4 && count < 8){
progressTag.removeClass('progress-bar-success');
progressTag.removeClass('progress-bar-danger');
progressTag.addClass('progress-bar-warning');
} else if (count >= 8){
progressTag.removeClass('progress-bar-warning');
progressTag.removeClass('progress-bar-success');
progressTag.addClass('progress-bar-danger');
}
countTag.text(count);
progressTag.css('width', count * 10 + "%");
}
function addVisit2(){
var countTag = $('#count2');
var count = countTag.text();
var progressTag = $('#percent2');
count ++;
if (count <= 4){
progressTag.removeClass('progress-bar-warning');
progressTag.removeClass('progress-bar-danger');
progressTag.addClass('progress-bar-success');
} else if (count > 4 && count < 8){
progressTag.removeClass('progress-bar-success');
progressTag.removeClass('progress-bar-danger');
progressTag.addClass('progress-bar-warning');
} else if (count >= 8){
progressTag.removeClass('progress-bar-warning');
progressTag.removeClass('progress-bar-success');
progressTag.addClass('progress-bar-danger');
}
countTag.text(count);
progressTag.css('width', count * 10 + "%");
}
function removeVisit2(){
var countTag = $('#count2');
var count = countTag.text();
var progressTag = $('#percent2');
count --;
if (count <= 4){
progressTag.removeClass('progress-bar-warning');
progressTag.removeClass('progress-bar-danger');
progressTag.addClass('progress-bar-success');
} else if (count > 4 && count < 8){
progressTag.removeClass('progress-bar-success');
progressTag.removeClass('progress-bar-danger');
progressTag.addClass('progress-bar-warning');
} else if (count >= 8){
progressTag.removeClass('progress-bar-warning');
progressTag.removeClass('progress-bar-success');
progressTag.addClass('progress-bar-danger');
}
countTag.text(count);
progressTag.css('width', count * 10 + "%");
}
function addVisit3(){
var countTag = $('#count3');
var count = countTag.text();
var progressTag = $('#percent3');
count ++;
if (count <= 4){
progressTag.removeClass('progress-bar-warning');
progressTag.removeClass('progress-bar-danger');
progressTag.addClass('progress-bar-success');
} else if (count > 4 && count < 8){
progressTag.removeClass('progress-bar-success');
progressTag.removeClass('progress-bar-danger');
progressTag.addClass('progress-bar-warning');
} else if (count >= 8){
progressTag.removeClass('progress-bar-warning');
progressTag.removeClass('progress-bar-success');
progressTag.addClass('progress-bar-danger');
}
countTag.text(count);
progressTag.css('width', count * 10 + "%");
}
function removeVisit3(){
var countTag = $('#count3');
var count = countTag.text();
var progressTag = $('#percent3');
count --;
if (count <= 4){
progressTag.removeClass('progress-bar-warning');
progressTag.removeClass('progress-bar-danger');
progressTag.addClass('progress-bar-success');
} else if (count > 4 && count < 8){
progressTag.removeClass('progress-bar-success');
progressTag.removeClass('progress-bar-danger');
progressTag.addClass('progress-bar-warning');
} else if (count >= 8){
progressTag.removeClass('progress-bar-warning');
progressTag.removeClass('progress-bar-success');
progressTag.addClass('progress-bar-danger');
}
countTag.text(count);
progressTag.css('width', count * 10 + "%");
}