AJN
AJN

Reputation: 1206

Progressbar behavior depending on checkboxes bootstrap + jquery

I have a series of questions, each associated with a checkbox, and the student can check his/her progress.

Desired behavior
- if the user check the questions (as done) the progress bar indicator increase with a certain value,
- But if unchecked, the progress bar indicator should decrease with the same value

Current behavior
Each time I click the checkbox (checked or unchecked), the progress bar increases.
And this works only when false and true are inversed, otherwise nothing happens

Following the code, something I am doing wrong.

<!DOCTYPE html>
<html>
<head>
 <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
    var progress = 0;

    $("#section1id").click(function(){
        $("#section1").toggle();
    });
    $("#section2id").click(function(){
        $("#section2").toggle();
    });

    $("#s11id").click(function(){
        if ($("#s11id").is(":checked") == false) {
        progress = progress+5;
        $('#blips > .progress-bar').attr("style","width:" + progress + "%");
        }
        else if ($("#s11id").is(":checked") == true) {
        progress = progress-5;
        $('#blips > .progress-bar').attr("style","width:" + progress + "%");
        }
    });
    $("#s21id").click(function(){
        if ($("#s12id").is(":checked") == false) {
        progress = progress+5;
        $('#blips > .progress-bar').attr("style","width:" + progress + "%");
        }
        else if ($("#s12id").is(":checked") == true) {
        progress = progress-5;
        $('#blips > .progress-bar').attr("style","width:" + progress + "%");
        }
});


});
</script>
  </head>


<body>

              <div class="checkbox">
                <label>
                  <input type="checkbox" class="section1-checkbox" id="section1id">Section1</label>
              </div>
              <div class="checkbox">
                <label>
                  <input type="checkbox" class="section2-checkbox" id="section2id">Section2</label>
              </div>


<div class="container">
  <hr>
  <div class="progress" id="blips">
    <div class="progress-bar" role="progressbar">
      <span class="sr-only"></span>
    </div>
  </div>
  
  <button class="btn btn-default">Stop</button>
</div>

<div class="panel panel-primary" id="section1">
              <div class="panel-heading">
                <h3 class="panel-title">Section1</h3>
              </div>
                <div class="panel panel-default">
                  <div class="panel-heading">
                    <class="checkbox" id="s11id">
                      <label>1.
                        <input type="checkbox" value="">
                      </label>
                    </class="checkbox">
                    <h4 class="panel-title">
                      <a data-toggle="collapse" data-parent="#accordion" href="#section11">

Question1-s1

              </a>
                    </h4>
                  </div>
                  <div id="section11" class="panel-collapse collapse">
                    <div class="panel-body">Answer-s1</div>
                  </div>
                </div>
              </div>
            </div>


<div class="panel panel-primary" id="section2">
              <div class="panel-heading">
                <h3 class="panel-title">Section2</h3>
              </div>
                <div class="panel panel-default">
                  <div class="panel-heading">
                    <class="checkbox" id="s21id">
                      <label>1.
                        <input type="checkbox" value="">
                      </label>
                    </class="checkbox">
                    <h4 class="panel-title">
                      <a data-toggle="collapse" data-parent="#accordion" href="#section21">

Question1-s2

              </a>
                    </h4>
                  </div>
                  <div id="section21" class="panel-collapse collapse">
                    <div class="panel-body">Answer-s2</div>
                  </div>
                </div>
              </div>
            </div>





</body>
</html>

Upvotes: 0

Views: 874

Answers (3)

Gobinath M
Gobinath M

Reputation: 2021

OnClick event are triggering which is on checkbox input and find checkbox is checked or not. based on checkbox checked value increase and decrees progress value.

JS:

var progress= 0;
function progressBar(e){
if(e.checked){
progress+=5;
alert(progress);
$('#blips > .progress-bar').attr("style","width:" + progress + "%");
}
else
{
progress-=5;
        alert(progress);
        $('#blips > .progress-bar').attr("style","width:" + progress + "%");
}
}

HTML:

<input type="checkbox" value="" id="s11id" onClick="progressBar(this)">

DEMO:

http://plnkr.co/edit/tGe70t90Y5EyvlgvjaVX?p=preview

Upvotes: 1

Mark Wilson
Mark Wilson

Reputation: 1354

PLNKR DEMO

http://plnkr.co/edit/7F3rdRwAAncyqXNeUfCV?p=preview

STACK SNIPPET

<!DOCTYPE html>
<html>
<head>
 <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>
jQuery(document).ready(function($){
    var progress = 0;

    $("#section1id").click(function(){
        $("#section1").toggle();
    });
    $("#section2id").click(function(){
        $("#section2").toggle();
    });

    $("#s11id input").on('change', function(){
        if ($("#s11id input").is(":checked") == true) {
        progress = progress+5;
        $('#blips > .progress-bar').attr("style","width:" + progress + "%");
        }
        else if ($("#s11id input").is(":checked") == false) {
        progress = progress-5;
        $('#blips > .progress-bar').attr("style","width:" + progress + "%");
        }
    });
    $("#s21id input").on('change', function(){
        if ($("#s21id input").is(":checked") == true) {
        progress = progress+5;
        $('#blips > .progress-bar').attr("style","width:" + progress + "%");
        }
        else if ($("#s21id input").is(":checked") == false) {
        progress = progress-5;
        $('#blips > .progress-bar').attr("style","width:" + progress + "%");
        }
    });


});
</script>
  </head>


<body>

              <div class="checkbox">
                <label>
                  <input type="checkbox" class="section1-checkbox" id="section1id">Section1</label>
              </div>
              <div class="checkbox">
                <label>
                  <input type="checkbox" class="section2-checkbox" id="section2id">Section2</label>
              </div>


<div class="container">
  <hr>
  <div class="progress" id="blips">
    <div class="progress-bar" role="progressbar">
      <span class="sr-only"></span>
    </div>
  </div>
  
  <button class="btn btn-default">Stop</button>
</div>

<div class="panel panel-primary" id="section1">
              <div class="panel-heading">
                <h3 class="panel-title">Section1</h3>
              </div>
                <div class="panel panel-default">
                  <div class="panel-heading">
                    <div class="checkbox" id="s11id">
                      <label>1.
                        <input type="checkbox" value="">
                      </label>
                    </div>
                    <h4 class="panel-title">
                      <a data-toggle="collapse" data-parent="#accordion" href="#section11">

Question1-s1

              </a>
                    </h4>
                  </div>
                  <div id="section11" class="panel-collapse collapse">
                    <div class="panel-body">Answer-s1</div>
                  </div>
                </div>
              </div>
            </div>


<div class="panel panel-primary" id="section2">
              <div class="panel-heading">
                <h3 class="panel-title">Section2</h3>
              </div>
                <div class="panel panel-default">
                  <div class="panel-heading">
                    <div class="checkbox" id="s21id">
                      <label>1.
                        <input type="checkbox" value="">
                      </label>
                    </div>
                    <h4 class="panel-title">
                      <a data-toggle="collapse" data-parent="#accordion" href="#section21">

Question1-s2

              </a>
                    </h4>
                  </div>
                  <div id="section21" class="panel-collapse collapse">
                    <div class="panel-body">Answer-s2</div>
                  </div>
                </div>
              </div>
            </div>
</body>
</html>

Changes made to your code:

  1. Used .is(":checked") for input. You were checking it for wrapper class. Also changed click event to on change event for input.

Changed

$("#s11id").click(function(){
        if ($("#s11id").is(":checked") == true) {

To

$("#s11id input").on('change', function(){
        if ($("#s11id input").is(":checked") == true) {
  1. Replaced invalid html with correct one

Changed

<class="checkbox" id="s11id">
  <label>1.
    <input type="checkbox" value="">
  </label>
</class="checkbox">

To

<div class="checkbox" id="s11id">
  <label>1.
    <input type="checkbox" value="">
  </label>
</div>

Upvotes: 1

Mahesh
Mahesh

Reputation: 320

Click event you are triggering which is not on checkbox input. And I don't think there is class tag in HTML. class is an attribute.

Updated your code.

<!DOCTYPE html>
<html>
<head>
 <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
    var progress = 0;

    $("#section1id").click(function(){
        $("#section1").toggle();
    });
    $("#section2id").click(function(){
        $("#section2").toggle();
    });

    $("#s11id").click(function(){
        if ($("#s11id").is(":checked") == false) {
        progress = progress-5;
        $('#blips > .progress-bar').attr("style","width:" + progress + "%");
        }
        else if ($("#s11id").is(":checked") == true) {
        progress = progress+5;
        $('#blips > .progress-bar').attr("style","width:" + progress + "%");
        }
    });
    $("#s21id").click(function(){
        if ($("#s12id").is(":checked") == false) {
        progress = progress-5;
        $('#blips > .progress-bar').attr("style","width:" + progress + "%");
        }
        else if ($("#s12id").is(":checked") == true) {
        progress = progress+5;
        $('#blips > .progress-bar').attr("style","width:" + progress + "%");
        }
});


});
</script>
  </head>


<body>

              <div class="checkbox">
                <label>
                  <input type="checkbox" class="section1-checkbox" id="section1id">Section1</label>
              </div>
              <div class="checkbox">
                <label>
                  <input type="checkbox" class="section2-checkbox" id="section2id">Section2</label>
              </div>


<div class="container">
  <hr>
  <div class="progress" id="blips">
    <div class="progress-bar" role="progressbar">
      <span class="sr-only"></span>
    </div>
  </div>

  <button class="btn btn-default">Stop</button>
</div>

<div class="panel panel-primary" id="section1">
              <div class="panel-heading">
                <h3 class="panel-title">Section1</h3>
              </div>
                <div class="panel panel-default">
                  <div class="panel-heading">

                      <label>1.
                        <input id="s11id" type="checkbox" value="">
                      </label>
                    <h4 class="panel-title">
                      <a data-toggle="collapse" data-parent="#accordion" href="#section11">

Question1-s1

              </a>
                    </h4>
                  </div>
                  <div id="section11" class="panel-collapse collapse">
                    <div class="panel-body">Answer-s1</div>
                  </div>
                </div>
              </div>
            </div>


<div class="panel panel-primary" id="section2">
              <div class="panel-heading">
                <h3 class="panel-title">Section2</h3>
              </div>
                <div class="panel panel-default">
                  <div class="panel-heading">

                      <label>1.
                        <input id="s21id" type="checkbox" value="">
                      </label>

                    <h4 class="panel-title">
                      <a data-toggle="collapse" data-parent="#accordion" href="#section21">

Question1-s2

              </a>
                    </h4>
                  </div>
                  <div id="section21" class="panel-collapse collapse">
                    <div class="panel-body">Answer-s2</div>
                  </div>
                </div>
              </div>
            </div>





</body>
</html>

Please try this https://jsfiddle.net/Le4yhmgs/

Upvotes: 1

Related Questions