user3875202
user3875202

Reputation: 31

How to get width in percentage using jquery

I am trying to get value of the width in percentage using j-Query. I don't want to set value of in j-Query.I had tried width() method and css('width').They both return not return the result in percentage.

Html-Code

style.css

.demo{
width:50%;
}

jquery Code

$(document).ready(function(){
var = $('.demo').css('width');//50px
var = $('.demo').width();//50
});

Thanks in advance for help.

Upvotes: 2

Views: 1792

Answers (1)

mohamedrias
mohamedrias

Reputation: 18566

$(function() {
  var parentWidth = $(document).width(); // parent element
  var demoWidth = ($(".demo").width()/parentWidth * 100).toFixed() +"%";
});

This would give you width in %

Upvotes: 3

Related Questions