0

I have a bootstrap modal having id myModal.

In my javascript when i do something like

window.onload = function(){
         var container = document.getElementById("myModal");
         console.log(container.clientWidth);
}

It shows 0. How to get the clientWidth of a Bootstrap modal before it is opened?

Jay Ghosh
  • 674
  • 6
  • 24
  • Can you share executable demo/snippet or [JSFiddle](https://jsfiddle.net/) ? [_Create a Minimal, Complete, and Verifiable example_](http://stackoverflow.com/help/mcve) – Rayon Sep 26 '16 at 05:27

1 Answers1

1

Jquery width works just fine

$('#ModalName').find('.modal-dialog').width()

For $('#ModalName') you are always getting wrong value.

user3272018
  • 2,309
  • 5
  • 26
  • 48
  • Well what do you know. jQuery works. But any idea as to why pure JS clientWidth is not working? – Jay Ghosh Sep 26 '16 at 05:42
  • In [this post](http://stackoverflow.com/questions/294250/how-do-i-retrieve-an-html-elements-actual-width-and-height) there is discussion abouth the same problem in comments for accepted answer. Maybe it will be usefull for you. – user3272018 Sep 26 '16 at 05:46
  • More details [here](http://stackoverflow.com/questions/1841124/find-the-potential-width-of-a-hidden-element) and [here](http://stackoverflow.com/questions/30637577/how-to-obtain-clientwidth-clientheight-before-div-is-visible). – user3272018 Sep 26 '16 at 05:54