0

I am trying to initialize multiple Progressbars, under the same class, with just one snippet of jQuery. I want the value to be set to the HTML Attribute "value".

Here's the html:

<div class="pop" value="98"></div>
<div class="pop" value="85"></div>
<div class="pop" value="78"></div>
<div class="pop" value="54"></div>

And here is the jQuery that I thought would work:

$( ".pop" ).progressbar({
value: parseInt($(this).attr("value"))
});

It didn't though. The progress bars appear but they look as if their value is 0.

If I change it to:

$( ".pop" ).progressbar({
value: parseInt($(".pop").attr("value"))
});

Then they all get initialized fine but all their value's get set to the value of the first HTML element (in this example, they would all get set to 98). I expected this.

So is there anyway to do this?

3 Answers 3

5

You want to use the jQuery.each function. Here's an example:

$.each($(".pop"), function() {
    $(this).progressbar({ value: parseInt($(this).attr("value")) });
});

Alternate example:

$(".pop").each(function() {
    $(this).progressbar({ value: parseInt($(this).attr("value")) });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks both to you and the answer above me!
1

I would loop through each div to make sure it's grabbing the right "value" attribute because the $(this) in your first example doesn't give you anything as this is the Window Object in that context.

$('.pop').each(function(index) {
    $(this).progressbar({
        value: parseInt($(this).attr("value"))
    });      
});

2 Comments

Thank you! May I ask why $this is the window and not the element?
the progressbar method doesn't set the "this" value which means it's going to default to whatever it is in your current scope, usually the Window object. With each() this is set to the current element/object your loop is on.
-2

You must set the value attribute:

var progressBar = $('#progressBar');
var v = 70;
progressBar.arrt('value', v);

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.