11

I'm trying to get the div width and height as the user changes it and submit that number to another page. I can't seem to figure out how to get the width and height though.

<script>
$(function() {
  $( "#set div" ).draggable({ 
    stack: "#set div",
    preventCollision: true,
    containment: $('#main_content'),
      stop: function(event, ui) {
          var mydiv = document.getElementById("set");
          var pos_x = ui.offset.left;
          var pos_y = ui.offset.top;

          var width = mydiv.style.width;        ----THIS DOESN'T WORK
          var height = mydiv.style.height;      ----THIS DOESN'T WORK

          var window_width = window.innerWidth;
          var window_height = window.innerHeight;
          var need = ui.helper.data("need");

          console.log(pos_x);
          console.log(pos_y);
          console.log(width);
          console.log(window_width);
          console.log(need);

          //Do the ajax call to the server
          $.ajax({
              type: "POST",
              url: "updatecoords.php",
              data: { x: pos_x, y: pos_y, need_id: need, width: width, height: height, window_width: window_width, window_height: window_height}
            }).done(function( msg ) {
              alert( "Data Saved: " + msg );
            });  
      }
  });
});
</script>

What's the proper way to do this?

1
  • no, those lines of code work fine. Commented May 9, 2013 at 21:52

4 Answers 4

24

It's quite wrong to use ele.style.width to get the element's width!!!!!!

In native JavaScript, you can get a element's CSS through two ways: ##Standard Method

window.getComputedStyle(ele)

For example,

var ele = document.getElementById("content"), // Do not use #
    eleStyle = window.getComputedStyle(ele);
/* Below is the width of ele */
var eleWidth = eleStyle.width;

##IE(IE 8 And Before)

element.currentStyle

For example,

var ele = document.getElementById("content"), // Do not use #
    eleStyle = ele.currentStyle;
/* Below is the width of ele */
var eleWidth = eleStyle.width;

##Why Not Use ele.style? ele.style is just get the attribule style of ele. If you use ele.style.width, you just get the width of ele.style, not the real width of ele.

If you have done something like:

ele.style.width = "55px"

You get "55px" when using ele.style.width. If you haven't, you will get undefined.

##How To Do In jQuery? Use $ele.width() (if you want the "exact" width, use $ele.outerWidth()), jQuery has done everything for you.

Sign up to request clarification or add additional context in comments.

Comments

15

In plain vanilla JavaScript use

var width = mydiv.offsetWidth;
var height = mydiv.offsetHeight;

This will give you numeric values, or

var width = mydiv.offsetWidth + 'px';
var height = mydiv.offsetHeight + 'px';

If you want them in "CSS" format.

1 Comment

For anyone wondering, offsetWidth is the faster of the two: jsperf.com/getcomputedstyle-vs-offsetwidth-offsetheight
7

Since you're already using jQuery, you can just do:

var width;

if (need == 1) {
   width = $("#web").width();
} else {
   width = $("#set").width();
}

2 Comments

in my code I have a var need. How would i tell the script that if need = 1, then var width= $("#web").width()
I've edited the answer to take into account the value of need.
1

Since you're using jQuery, you'll probably want to know about the following:

$('#id').outerWidth() $('#id').outerWidth(true)

These will come in very handy. This allows you to find the total width of the div (padding, border, width and (optional argument) margin).

http://api.jquery.com/outerWidth/

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.