0

I need to call a javascript function to run a progress bar, here is my code:

<form name="Calculation" method="post">
<progress id="progressBar" value="0" max="100" style="width:300px;"> </progress>
<span id="status"></span> 
<h1 id="finalMessage"></h1> 
<input type="button" id="btn2" value="Press me!" onclick="function progressBarSim(al)"/>
</form>
 <script>
function progressBarSim(al) {
 var bar = document.Calculation.getElementById('progressBar');
 var status = document.Calculation.getElementById('status');
 status.innerHTML = al + "%";
 bar.value = al;
 al++;
 var sim = setTimeout("progressBarSim(" + al + ")", 1);
 if (al == 100) {
 status.innerHTML = "100%";
 bar.value = 100;
 clearTimeout(sim);
 var finalMessage = document.getElementById('finalMessage');
 }
 }
 var amountLoaded = 0;
 progressBarSim(amountLoaded);
 </script>

The javascript function works on its own but I need to run it when the button is pressed and nothing happnes when I press the button "Press me".! any idea what's wrong? Thanks

asked Jul 23, 2015 at 16:23
4
  • 2
    onclick="progressBarSim()", however what are you passing as the argument you have in your function? Also document.Calculation is incorrect. Use document.getElementById('progressBar') Commented Jul 23, 2015 at 16:26
  • Calculation is the name of the form Commented Jul 23, 2015 at 16:41
  • And there's no need at all to use it as you are using getElementById to reference the ID of your element. Commented Jul 23, 2015 at 16:42
  • OK, I removed it but it did not solve my problem. The press button still does not work. Thank you though. Commented Jul 23, 2015 at 16:45

6 Answers 6

1

You need to call the function in "onclick", instead of writing "function progresBarSim()". Also, "document.Calculation.getElementById" produces a js error, you should write "document.getElementById". Try this:

<form name="Calculation" method="post">
<progress id="progressBar" value="0" max="100" style="width:300px;"> </progress>
<span id="status"></span> 
<h1 id="finalMessage"></h1> 
<input type="button" id="btn2" value="Press me!" onclick="progressBarSim(0)"/>
</form>
 <script>
function progressBarSim(al) {
 var bar = document.getElementById('progressBar');
 var status = document.getElementById('status');
 status.innerHTML = al + "%";
 bar.value = al;
 al++;
 var sim = setTimeout("progressBarSim(" + al + ")", 1);
 if (al == 100) {
 status.innerHTML = "100%";
 bar.value = 100;
 clearTimeout(sim);
 var finalMessage = document.getElementById('finalMessage');
 }
 }
 var amountLoaded = 0;
 progressBarSim(amountLoaded);
 </script>
answered Jul 23, 2015 at 16:56
Sign up to request clarification or add additional context in comments.

4 Comments

It works, thank you. But why the argument is zero?? progressBarSim(0) instead of progressBarSim(al)
Another question, the progress bar runs when I refresh the page. I want it to ONLY run when I press the button. Is is any way to correct this?
Then don't call the function when the page loads (e.g. progressBarSim(amountLoaded);). I highly suggest you read some introduction to JavaScript tutorials.
The zero argument means the progress bar starts from zero percent. To not let it run on page refresh, do what j08691 suggests and delete these lines: var amountLoaded = 0; progressBarSim(amountLoaded); Learning some basic JavaScript will help you a lot:)
1

I fiddled around with the code and changed few stuff, try the following code:

<form name="Calculation" method="post">
 <progress id="progressBar" value="0" max="100" style="width:300px;"></progress>
 <span id="status"></span> 
 <h1 id="finalMessage"></h1> 
 <input type="button" id="btn2" value="Press me!" onclick="progressBarSim(0)"/>
</form>
<script>
 function progressBarSim(al) {
 var bar = document.getElementById('progressBar');
 var status = document.getElementById('status');
 status.innerHTML = al + "%";
 bar.value = al;
 al++;
 var sim = setTimeout(function(){ progressBarSim(al); }, 1);
 if (al == 100) {
 status.innerHTML = "100%";
 bar.value = 100;
 clearTimeout(sim);
 var finalMessage = document.getElementById('finalMessage');
 }
}
</script>
answered Jul 23, 2015 at 17:11

Comments

0

Change

onclick="function progressBarSim()"

to

onclick="progressBarSim()"
answered Jul 23, 2015 at 16:25

Comments

0

Replace <input type="button" id="btn2" value="Press me!" onclick="function progressBarSim()"/> with

<input type="button" id="btn2" value="Press me!" onclick="progressBarSim()"/>
answered Jul 23, 2015 at 16:25

1 Comment

Doesn't work, and yes there is one argument al that should be defined.
0

Remove function like onclick="ProgressBarSlim()"

answered Jul 23, 2015 at 16:26

Comments

0

Replace onclick="function progressBarSim()" with onclick="progressBarSim()"

Like this:

<input type="button" id="btn2" value="Press me!" onclick="function progressBarSim()"/>

After replaced

<input type="button" id="btn2" value="Press me!" onclick="progressBarSim()"/>
answered Jul 23, 2015 at 16:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.