title: Javascript width of span element
body: <p>I create via a String some DOM-Elements and assign it via innerHTML to a div.
  Now I need a flexible width of the span element (span_homebase). What I experience is that sometimes (for me it looks randomly) it returns other width than expected. I need the width of this span element because after this in the same line there is an Information Button but the width of the span could be different. So I use the width of span element as margin for the information button. Code is as follows. I searched for a async handler of innerHTML but in another post somebody said that it is not required because it is assigned instantly.
  Are there any thoughts what to try? Because the information is now sometimes on the right place and sometimes not.</p>
  
  <pre><code>string = string + "&lt;span id ='span_homebase'&gt;" + homebaseCity + "&lt;/span&gt;";
  string = string + "&lt;div class='section-end'&gt;&lt;/div&gt;";
  element.innerHTML = string;
  var marginInfoHomebase = document.getElementById('span_homebase').offsetWidth + 20;
  document.getElementById("information_button_homebase").style.margin = "5px 0 0 " + marginInfoHomebase + "px";
  </code></pre>
  
  <p>Now if I open the respective site for me sometimes the marginInfoHomebase sometimes returns 108 and sometimes 183 even if the same value is assigned to span element. Very strange. Any thoughts?</p>
  
  <p>EDIT:
  Working Example:</p>
  
  <pre><code>function createHomeSettingsView(settingsA){
      var element = document.getElementById("table-settings");
      var string = "";
      for(var i = 0; i &lt; settingsA.length; i++){
          for(var z = 0; z &lt; settingsA[i].length; z++){
              if(i == 1){
                  //Notification buttons
                  if(z == 1){
                      //homebase setting
                      if(window.localStorage.getItem("switch_homebase") == 1){
                          //homebase on
                          var homebaseCity = settingsA[i][z] + ": " + window.localStorage.getItem("homebase_loc");
                          string = string + " \
                          &lt;div class='row' style='height:100px;' id='settings-sec-"+ i +"-row-" + z +"'&gt; \
                              &lt;div class='text'&gt; \
                                  &lt;span id='span_homebase'&gt;" + homebaseCity + "&lt;/span&gt; \
                              &lt;/div&gt; \
                              &lt;div onClick='homebaseInfoClick();' class='information_button' id='information_button_homebase'&gt;&lt;/div&gt;\
                              &lt;div id='handleAutoSwitcherHomebase'&gt;&lt;/div&gt;\
                              &lt;div id='showRadiusRangeHomebase'&gt;Radius: 30km&lt;/div&gt;\
                              &lt;input class='sliders' id='changeRadiusRangeHomebase' type='range' min='5' max='100' step='5' oninput='handleChangeHomebase(this.value)' onchange='handleInputHomebase(this.value)' &gt;\
                          &lt;/div&gt;";
                      }
                      else{
                          //homebase off
                          string = string + " \
                          &lt;div class='row' id='settings-sec-"+ i +"-row-" + z +"'&gt; \
                              &lt;div class='text'&gt;\
                                  &lt;span id='span_homebase'&gt;" + settingsA[i][z] + "&lt;/span&gt; \
                              &lt;/div&gt; \
                              &lt;div onClick='homebaseInfoClick();' class='information_button' id='information_button_homebase'&gt;&lt;/div&gt;\
                              &lt;div id='handleAutoSwitcherHomebase'&gt;&lt;/div&gt;\
                          &lt;/div&gt;";
                      }
                  }
              }           
          }
      }
      element.innerHTML = string;
      var marginInfoHomebase = document.getElementById("span_homebase").offsetWidth + 25;
      var marginText = "5px 0 0 " + marginInfoHomebase + "px";
      console.log("Span: " + marginInfoHomebase);
      document.getElementById("information_button_homebase").style.margin = marginText;
  }
  </code></pre>
  
  <p>CSS:</p>
  
  <pre><code>.container .table-settings{
      top: 64px;
      position: absolute;
      padding:0;
      left: 0;
      right: 0;
      bottom:0;
      overflow-y: scroll;
      -webkit-overflow-scrolling: touch;
      background-color: #ECEBF3;
  }
  
  .container .table-settings .row{
      background-color: white;
      padding-top: 14px;
      height:50px;
      border-bottom: 1px solid LightGray;
  }
  
  .container .table-settings .row .text{
      margin-left: 15px;
      color: black;
      float: left;
  }
  
  #span_homebase{
      display: inline-block;
  }
  </code></pre>