2

I have a progressbar on my website and this is a part of its css:

.progress-bar span {
    display: inline-block;
    height: 25px;
    width: 78%; //the actual position

    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    border-radius: 3px;

   //other css
}

I have to change the width that indicates the position of the progression (the line you see here). This is the first time I use progress bars and I have no idea about how to setup the javascript code.

Any advice? jsfiddle

2
  • can you please give us more information about your javascript code and the rest of your program / site / script ? (Maybe a small jsFiddle) Commented Aug 22, 2013 at 13:17
  • jsfiddle.net/WrYKf added Commented Aug 22, 2013 at 13:19

4 Answers 4

3

Here is the way without jQuery

HTML

<div  class="progress-bar" align="left">
    <span id="prog"></span>
</div>

JS:

var elem = document.getElementById("prog");
elem.style.width = "88%";
Sign up to request clarification or add additional context in comments.

Comments

1

I'd recommend using jQuery to make your javascript calls much simpler. You can download the latest version here: http://jquery.com/download/

First, include the jQuery file in the head of your html like this:

<head>
...
<script type="text/javascript" src="<location of file>jquery-1.10.2.min.js"></script>
...
</head>

Then within your own javascript code, you can move the position of the progress bar like so:

jQuery('.progress-bar scan').css('width', <desired width>);

Hopefully that helps.

2 Comments

Is there a non jQuery way? +1
Sure, you can do it like this: var elems = document.getElementsByTagName('span'), i; for (i in elems) { if(elems[i].parentNode.className === 'progress-bar') { elems[i].style.width = <desired width>; } }
0

You can use:

document.querySelectorAll('.progress-bar span').forEach(function(elem) {
    elem.style.width = '<YOUR WIDTH>'; 
});

no jQuery

Comments

0

I just incremented the width this way:

function increment(){
var width = document.getElementById('progressbar').clientWidth;
var divArray = document.getElementById('progressbar');
if(width<500){
var largura = width+10;
divArray.style.width = largura+'%';
setTimeout("increment()",1000);
}
else{
window.location.href = "http://www.google.com/";
}
}

And does it every second so it will be incremented every second but u can change it the way you want.

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.