0

I don't understand why the following code return that terminalContainer is null:

var terminalContainer = document.getElementById('terminal-container');
console.log("terminalContainer:" + terminalContainer);

But in my HTML I defined it :

<li class="liContainer">
        <div>
            <h3>{{nameContainer}}</h3>
        </div>
        <div id="idContainer">
            <span>ID: {{idContainer}}</span>
        </div>
        <div id="stateContainer">
            <span class="state">State: {{stateContainer}}</span>
        </div>

        <div id="terminal-container"></div>
        <div class="terminal"></div>


          <button type="button" class="stop {{#if to_hide_stop}}hidden{{/if}}"> </button>
          <button type="button" class="start {{#if to_hide_start}}hidden{{/if}}"> </button>
          <button type="button" class="pause {{#if to_hide_pause}}hidden{{/if}}"></button>
          <button type="button" class="unpause {{#if to_hide_unpause}}hidden{{/if}}"> </button>
          <button type="button" class="cmdLogs"> </button>
    </li>

And the result on my app is that I have 4 <li> so the message "terminalContainer: null" appear 4 times in the console, that means my div exists right ?

What am I not understanding ?

Jerome
  • 1,032
  • 10
  • 27
  • That's why I hate Meteor, everytime I post something about a problem I don't know why but it works in JSFiddle... Yes it is because if I did it before it's executed I got 1 error only @plvice – Jerome Jan 17 '17 at 08:02
  • @AndersTornblad & insertusernamehere - Even if he/she did, `getElementById` would return one of them (the first, in my experience, but of course it's not defined). – T.J. Crowder Jan 17 '17 at 08:02
  • @AndersTornblad yes I have 4 `
  • `that contains each one 1 `
    ``
  • – Jerome Jan 17 '17 at 08:02
  • @T.J.Crowder True that, but the OP should be aware of it, because it will create other problems later. – insertusernamehere Jan 17 '17 at 08:03
  • That is not the most probable cause of your problem, but it is a bad design that will definitely confuse your JavaScript. – Anders Marzi Tornblad Jan 17 '17 at 08:03
  • 1
    Should I use class instead of id ? – Jerome Jan 17 '17 at 08:05
  • @Jerome: If you have multiple elements that you want to act on as a group, yes. ID values in documents **must** be unique. – T.J. Crowder Jan 17 '17 at 08:07
  • Using class + waiting 200ms resolved my problem – Jerome Jan 17 '17 at 08:09