Link to home
Start Free TrialLog in
Avatar of kenjpete
kenjpete

asked on

Need Help With jQuery Progress Bar

I am a jQuery novice and I am trying to build a dynamic jQuery progress bar. What I need to do is offer a series of checkboxes on a page so that when the visitor checks or unchecks a checkbox it will increase or decrease the value shown on the progress bar. In addition, I need to alert the visitor when they have reached the maximum amount (percentage). Any help would be appreciated.

Here is what I have:

Javascript:

<head>

<link type="text/css" href="css/ui-lightness/jquery-ui-1.8.18.custom.css" rel="Stylesheet" />
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.18.custom.min.js"></script>
<script type="text/javascript">
       $(document).ready(function() {
           $("#budgetbar").progressbar({ value: 0 });        
          $(".option1").observe('click', progressbar({value: 10});
          $(".option2").observe('click', progressbar({value: 50});          
          $(".option3").observe('click', progressbar({value: 20});
          $(".option4").observe('click', progressbar({value: 50});
          $("#budgetbar").progressBar({ max: 1000, textFormat: 'fraction', callback: function(data) { if (data.running_value == data.value) { alert("Budget limit reached!"); } }} );

});
</script>

</head>


HTML:


<body>

<div id="budgetbar"></div>  
<div>    
    <input type="checkbox" class="option1" />Expense 1 - $100,000<br />
    <input type="checkbox" class="option2" />Expense 2 - $500,000<br />
    <input type="checkbox" class="option3" />Expense 3 - $200,000<br />
    <input type="checkbox" class="option4" />Expense 4 - $500,000<br />
    * Max Expenses - $1,000,000
</div>  

</body>
Avatar of mcnute
mcnute
Flag of Germany image

According to your values given by the jquery code above, your max value in the progressbar should be 100 and not 1000.
Avatar of kenjpete
kenjpete

ASKER

I changed that value to 100 but the script still isn't working. Any other ideas or thoughts?
I modified the jquery code to the following and now the progress bar is bound to the click event, but I'm still not sure how to increment the value if multiple boxes are checked or decrease the value when a checkbox is unchecked?

Here is the modified jquery code:


$(document).ready(function() {
           $("#budgetbar").progressbar({ value: 0 });
           $(".option1").click(function () {
                         $("#budgetbar").progressbar({ value: 10 });

           });
           $(".option2").click(function () {
                         $("#budgetbar").progressbar({ value: 50 });

           });
               $(".option3").click(function () {
                         $("#budgetbar").progressbar({ value: 20 });

           });
           $(".option4").click(function () {
                         $("#budgetbar").progressbar({ value: 50 });

           });
       
       //$("#budgetbar").progressBar({ max: 100, textFormat: 'fraction', callback: function(data) { if (data.running_value == data.value) { alert("Budget limit reached!"); } }} );

});
Try to check for the :checked selector.

like this:

$('budgetbar').click(function(){
var progBar = '';
$('input:checked').each(function(){
progBar += $(this).val();
});

budgetBar(progBar);
});

function budgetBar(progBar) {

// here update the progressBar and check if the :checked values reached 100 or more >= operator

}

Open in new window


If you could give us a link where to see your code, we can help you even more.
ASKER CERTIFIED SOLUTION
Avatar of kenjpete
kenjpete

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well, it is not all about posting the solution to authors here. As for me, I try to suggest the way to solve a problem but not giving a full solution to people. This helps people for future problems also, and not only solving their problems in a short term perspective. Good luck with your projects.
I accepted my own solution because none of the expert responses worked.