Solved

Need Help With jQuery Progress Bar

Posted on 2012-04-05
7
611 Views
Last Modified: 2012-04-16
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>
0
Comment
Question by:kenjpete
  • 4
  • 3
7 Comments
 
LVL 11

Expert Comment

by:mcnute
ID: 37815332
According to your values given by the jquery code above, your max value in the progressbar should be 100 and not 1000.
0
 

Author Comment

by:kenjpete
ID: 37816152
I changed that value to 100 but the script still isn't working. Any other ideas or thoughts?
0
 

Author Comment

by:kenjpete
ID: 37816784
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!"); } }} );

});
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Expert Comment

by:mcnute
ID: 37818799
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.
0
 

Accepted Solution

by:
kenjpete earned 0 total points
ID: 37832553
I have solved this myself, so that others can use the code if needed, here it is:


<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() {

    $(function() {

    $("#budgetbar").progressbar({

        value: 0,

        max: 100,

        textFormat: 'fraction'

    });

    $('.option').on('change', function() {

        var total = 0;

        $('.option:checked').each(function() {

            total += parseInt($(this).val());

        });


        if (total == 100) {

            alert("You have closed the budget shortfall!");

        } else if (total > 100) {

            alert("Your last selection has caused you to exceed the necessary $3 million budget shortfall!")

        }

        $("#budgetbar").progressbar("value", total);

    });

});

});

</script>


<body>
  <div id="budgetbar"></div>  
<div>    
    <input type="checkbox" class="option" />Expense 1 - $100,000<br />
    <input type="checkbox" class="option" />Expense 2 - $500,000<br />
    <input type="checkbox" class="option" />Expense 3 - $200,000<br />
    <input type="checkbox" class="option" />Expense 4 - $500,000<br />
    * Max Expenses - $1,000,000
</div>  
</body>
0
 
LVL 11

Expert Comment

by:mcnute
ID: 37832594
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.
0
 

Author Closing Comment

by:kenjpete
ID: 37850294
I accepted my own solution because none of the expert responses worked.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now