javascript - How to know if a DIV width is set in Percentage or Pixel using jQuery -
is possible know if div's width set in percentage or pixel.
if (widthsetaspixel) { // code doing respect pixel } if (widthsetaspercentage) { // code doing respect percentage }
you don't want use jquery give width in px if it's set in percent (or other unit). so, in order unit of width you'll want use window.getcomputedstyle
. problem still gives width in px unless element has display: none;
so... width in actual unit use function wrote:
function getstyle(el){ var display = window.getcomputedstyle(el).getpropertyvalue("display"); el.style.display = "none"; var width = window.getcomputedstyle(el).getpropertyvalue("width"); el.style.display = display; return width; }
then check whether or not width percent this:
var element = document.getelementbyid("myelementid"); var widthispercent = getstyle(element).indexof("%") > -1;
this principle true other css properties height
, left
, etc.
jsfiddle of in action: http://jsfiddle.net/dwjqflsz/
Comments
Post a Comment