Reputation: 211
i have this code:
HTML
<div class="bar-value"></div>
CSS
.bar-value{
width: 50%;
height: 28px;
background: #8BBDE8;
}
JQUERY
$(document).ready(function(){
var percent = $('.bar-value').css('width');
});
the problem is that this returns the width of the div in pixels (in this case: 200px). I want that to return the value of the div in percentage (50%).
Thanks in advance!
Upvotes: 1
Views: 2552
Reputation: 25634
To get a percentage, you need the width of it's parent. Try this :
$(document).ready(function(){
var element = $('.bar-value');
var percent = element.css('width')/element.parent().width()*100+'%';
});
Upvotes: 3