How to determine the width of a block in javascript? for some reason my offsetWidth and clientWidth do not work (

  • on pure js, or is jQuery valid? - Max Zhukov
  • through the console it turns out, but through the file there is no (what could be the problem? (Uncaught TypeError: Cannot read property 'offsetWidth' of null) the error that is output - Dikaz
  • This error means that you are trying to read the object that is not there. check your code. or lay out on jsfiddle. - Max Zhukov
  • but why then from the console works? - Dikaz
  • Do you offer one line of error to determine what is happening? mb you have not created an object at the time of the request ... - Max Zhukov

1 answer 1

most likely your problem is that at the time of the execution of this script, the objects have not yet been created to create. declare your function to work with objects in $ (document) .ready (...)

UPD:

document.addEventListener('DOMContentLoaded', function() { //код }); 
  • but I am not writing in jquery, how to do it in pure javascript? - Dikaz
  • a) all) just wrote a script to the end) - Dikaz