Reputation: 163
I'm trying to do something so trivial and simple but yet been bumping my head for well an hour. I'm trying to make a progress bar move forward by adding 10 every time user clicks on button but only the first click works. I think I need a second pair of eyes because I'm not seeing where I'm wrong. Obviously I'm wrong somewhere.
I have reproduced the use case here :
$(document).ready(function() {
$('#increaseMeter').click(function(e){
e.preventDefault();
//get meter progress number
var progress = $('.barmeter').attr('meterprogress');
//add 10 to the meter
var mathnum =progress;
mathnum += parseInt(progress)+parseFloat(10);
//format new classes depending on progress number
if(progress <60){
var newclass = 'progress-bar-warning';
}else{
var newclass = 'progress-bar-success';
}
//update meter progress attribute
$('.barmeter').attr('meterprogress='+mathnum);
//add class to meter
$('.barmeter').addClass(newclass);
//change style width
$('.barmeter').attr('style', 'width:'+mathnum+'%');
//change aria value
$('.barmeter').attr('aria-valuenow', mathnum);
//display new text
$('.metertext').html(mathnum+'%');
});
});
<!-- bootstrap css stylesheets -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- bootstrap javascript, jquery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<p>
<br><br>
<div class="container">
<button class="btn btn-lg btn-primary"
id="increaseMeter">
Increase Meter
</button>
</p> <br><br>
<div class="col-md-6">
<div style="" class="salesmeter">
Meter Probability
<div class="progress">
<div class="progress-bar barmeter"
meterprogress="0"
role="progressbar"
aria-valuenow="0"
aria-valuemin="0"
aria-valuemax="100">
<span class="metertext"></span>
</div> </div></div>
Upvotes: 3
Views: 515
Reputation: 123
$(document).ready(function() {
$('#increaseMeter').click(function(e){
e.preventDefault();
//get meter progress number
var progress = parseInt($('.barmeter').attr('meterprogress'));
//add 10 to the meter
var mathnum = progress + parseFloat(10);
//format new classes depending on progress number
if(progress <60){
//add class to meter
$('.barmeter').addClass('progress-bar-warning');
}else{
$('.barmeter').addClass('progress-bar-success').removeClass('progress-bar-warning');
}
//update meter progress attribute
$('.barmeter').attr('meterprogress' , mathnum);
//change style width
$('.barmeter').attr('style', 'width:'+mathnum+'%');
//change aria value
$('.barmeter').attr('aria-valuenow', mathnum);
//display new text
$('.metertext').html(mathnum+'%');
});
});
https://codepen.io/vommbat/pen/LBBpRz/
Upvotes: 3