0

I'm trying to change class after progressbar is full, but the class doesn't change and I get no error. It remains the class show. Here is my code - what is wrong with it?:

function animate_progressbar() {
  $total_width = $("#progressbar_wrapper").width();
  $width_inc = $total_width / 1;

  if ($("#progressbar").width() < $total_width) {
    $width = $("#progressbar").width() + $width_inc;
    $("#progressbar").animate({
      width: '' + $width + ''
    }, 7000);
  }

  if ($("#progressbar").width() == $total_width) {
    $('#progressbar').removeClass("show");
    $('#progressbar').addClass('hider');
  }

}

function reset_progressbar() {
  $("#progressbar").animate({
    width: '0px'
  }, 300);
  $('#progressbar').addClass('show');
  $('#progressbar').removeClass('hider');
}
body {
  margin: 0 auto;
  padding: 0px;
  text-align: center;
  width: 100%;
  font-family: "Myriad Pro", "Helvetica Neue", Helvetica, Arial, Sans-Serif;
  background-color: #A9F5A9;
}

#wrapper {
  margin: 0 auto;
  padding: 0px;
  text-align: center;
  width: 995px;
}

#progressbar_wrapper {
  border: 1px solid #088A08;
  margin-left: 345px;
  margin-top: 20px;
  width: 300px;
  height: 35px;
  border-radius: 3px;
  overflow: hidden;
}

#progressbar {
  width: 0px;
  height: 35px;
  border-radius: 0px;
  background-color: green;
}

#progressbar_div input[type="button"] {
  background-color: #088A08;
  border: none;
  width: 150px;
  height: 40px;
  color: white;
  border-radius: 3px;
  border-bottom: 3px solid #0B610B;
}

.hider {
  opacity: 0;
}

.show {
  opacity: 100;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div id="wrapper">
  <div id="progressbar_div">
    <div id="progressbar_wrapper">
      <div id="progressbar" class="show"></div>
    </div>
    <p>
      <input type="button" value="ANIMATE" onclick="animate_progressbar();">
      <input type="button" value="RESET" onclick="reset_progressbar();">
    </p>
  </div>
</div>

I was trying with

if($("#progressbar").width() < $total_width)
             {
            $width=$("#progressbar").width() + $width_inc;
            $("#progressbar").animate({width:''+$width+''},7000);
            }
            
            else
             {
            $('#progressbar').removeClass("show");
            $('#progressbar').addClass('hider');
            }

But no effect - still class show all the time.

4
  • You are opening your <script> tag but not closing it. Therefore, the browser interprets everything after it as Javascript, even the HTML. Doesn't your console say Uncaught SyntaxError: Unexpected token '<' ? Commented Dec 4, 2021 at 14:36
  • it is closed, my copypaste error. But thank you! Commented Dec 4, 2021 at 14:36
  • It would have been nice if you had gone the extra step to make this a complete demo so we can see what is going on and debug it in dev tools. See I've been told to create a "runnable" example with "Stack Snippets", how do I do that? Commented Dec 4, 2021 at 14:40
  • did it, thank you for idea Commented Dec 4, 2021 at 14:43

1 Answer 1

1

function animate_progressbar() {
  $total_width = $("#progressbar_wrapper").width();
  $width_inc = $total_width / 1;

  if ($("#progressbar").width() < $total_width) {
    $width = $("#progressbar").width() + $width_inc;
    $("#progressbar").animate({
      width: '' + $width + ''
    }, 7000, function() {
        $('#progressbar').removeClass("show");
        $('#progressbar').addClass('hider');
    });
  }

 

}

function reset_progressbar() {
  $("#progressbar").animate({
    width: '0px'
  }, 300);
  $('#progressbar').addClass('show');
  $('#progressbar').removeClass('hider');
}
body {
  margin: 0 auto;
  padding: 0px;
  text-align: center;
  width: 100%;
  font-family: "Myriad Pro", "Helvetica Neue", Helvetica, Arial, Sans-Serif;
  background-color: #A9F5A9;
}

#wrapper {
  margin: 0 auto;
  padding: 0px;
  text-align: center;
  width: 995px;
}

#progressbar_wrapper {
  border: 1px solid #088A08;
  margin-left: 345px;
  margin-top: 20px;
  width: 300px;
  height: 35px;
  border-radius: 3px;
  overflow: hidden;
}

#progressbar {
  width: 0px;
  height: 35px;
  border-radius: 0px;
  background-color: green;
}

#progressbar_div input[type="button"] {
  background-color: #088A08;
  border: none;
  width: 150px;
  height: 40px;
  color: white;
  border-radius: 3px;
  border-bottom: 3px solid #0B610B;
}

.hider {
  opacity: 0;
}

.show {
  opacity: 100;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div id="wrapper">
  <div id="progressbar_div">
    <div id="progressbar_wrapper">
      <div id="progressbar" class="show"></div>
    </div>
    <p>
      <input type="button" value="ANIMATE" onclick="animate_progressbar();">
      <input type="button" value="RESET" onclick="reset_progressbar();">
    </p>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

Using the animate callback is likely what OP wants but you should provide an explanation about your solution. Code only answers are not very helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.