0

The following script written for a progress bar, is assigned to a div with id=container1. This is while I have several divs with ids like container2, container3 and ... which all should be referred to this script.

How can I assign this script to all divs?

var bar = new ProgressBar.Line(container1, {
  strokeWidth: 4,
  easing: 'easeInOut',
  duration: 1400,
  color: '#66cc33',
  trailColor: 'transparent',
  trailWidth: 1,
  svgStyle: {
    width: '100%',
    height: '100%',
    position: 'absolute'
  }
});

bar.animate(1.0); // Number from 0.0 to 1.0
4
  • May I know where is the definition for ProgressBar.Line?? Commented Aug 16, 2016 at 8:01
  • look through call and apply in javascript Commented Aug 16, 2016 at 8:03
  • The ProgressBar.Line code is acting on the division by id="container1", I want it to work on divisions with id: container2, container3 and ... as well. Commented Aug 16, 2016 at 8:04
  • @Bálint Division is a real term. Commented Aug 16, 2016 at 8:07

3 Answers 3

1

If I understand your question correctly you want several progressBar instances referenced to the same options object.

var progressBarOptions={
  strokeWidth: 4,
  easing: 'easeInOut',
  duration: 1400,
  color: '#66cc33',
  trailColor: 'transparent',
  trailWidth: 1,
  svgStyle: {
    width: '100%',
    height: '100%',
    position: 'absolute'
  }
}

var bar1 = new ProgressBar.Line(container1, progressBarOptions);
var bar2 = new ProgressBar.Line(container2, progressBarOptions);
var bar3 = new ProgressBar.Line(container3, progressBarOptions);
Sign up to request clarification or add additional context in comments.

Comments

0

If you don't use closures, then each of these containers are accessible from the window object and you can use array access to get the objects:

var containerCount = 5;
for (var i = 1; i <= containerCount; i++) {
    var bar = new ProgressBar.Line(window["container" + i, {
        // Stuff here
    }
}

If you do use closures then you need to put these in an object and use the same thing but replacing the window with the object's name.

Comments

0

It depends on the API of ProgressBar. You have to read the API document or the definition of ProgressBar.Line to figure out if new ProgressBar.Line(option) could return the same instance each time.

Comments

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.