11

I want to get the width of an element in percentage. I am using the following function to get the style but it gives the value in pixels. Here the variable style can be any css property like width, height etc.

this.getStyle = function(style) {
    var elementStyle = window.getComputedStyle(that.element);
    var value = elementName.getPropertyValue(style);
    return value;
}
4
  • How do you set the width? Commented Dec 20, 2015 at 6:32
  • What about doing a manual calculation of the % using the pixel width value of the parent element? Commented Dec 20, 2015 at 6:33
  • i have set the width in percentage as 'width: 100%' Commented Dec 20, 2015 at 6:33
  • 3
    Possible duplicate: stackoverflow.com/questions/9730612/… Commented Dec 20, 2015 at 6:34

3 Answers 3

13

Seems that you're looking for

that.element.style.width
that.element.style.height
etc
Sign up to request clarification or add additional context in comments.

2 Comments

It's working only if the css is inline style like: <div style="width:60%">test</div>
@MoshFeu it works even if CSS is not defined inline with latest Firefox and Chromium (tested with Firefox 56 and Chromium 62). But I think it depends of the CSS property, for example when using a CSS stylesheet, I can access width and height via element.style but not for the border property: stackoverflow.com/questions/10675885/…
3

I think you get always pixels, rather than % width. The reason is while object render it always set physical width based on% we given, this you can verify via dom. Javascript use DOM(Document Object Model), while jQuery you can use Dom as well as before load property via document.getready().

So as above you can get the property, but in pixels.

document.getElementById('yourdivname').element.style.width

or

div2 = document.getElementById('div2');
alert("Width of div2 with style = " + div2.style.width);

Getting the width of an html element in percent % with jQuery

This is interesting :

  1. Is it possible to use jQuery to get the width of an element in percent or pixels, based on what the developer specified with CSS?

  2. http://www.lucemorker.com/blog/javascript-vs-jquery-quick-overview-and-comparison

$(document).ready is a jQuery event to be triggered after the HTML document has been loaded vs onload is a built-in DOM event to be triggered after all content has been loaded. So the ready event would normally fire earlier than the onload event, allowing code execution as early as possible without having to wait for all assets to be fully loaded

For more details :- click this link.

Comments

-1

Have you tried something like this:

var width = (100 * parseFloat($('.largeField').css('width')) / parseFloat($('.largeField').parent().css('width')) ) + '%';

2 Comments

how do you write this in pure javascript?
The Original Poster asked for pure JavaScript, but this is jQuery Code without explanation.

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.