title: javascript: onclick go to specific div
body: <p>I'm trying to create a "diagram" with divs. If user click on next it will show the next div and so on (this is done), but the thing i'm trying to reach is, sometimes some divs will have a question with "yes" or "no" buttons, and those buttons will target to some specific div.</p>
  
  <p>Ex: Div 1 - Are you ok ? Yes - Go to Div 2 | No - Go to Div 3.</p>
  
  <p>Is there a way to make this dynamic ? All divs have an ID.</p>
  
  <p>Here's the code i've got so far.</p>
  
  <p>HTML</p>
  
  <pre><code> &lt;div id="main"&gt;   
    &lt;h3 class="despistes"&gt;Some Title&lt;/h3&gt;
        &lt;div class="info" id="1" style="display:block"&gt;Div 1&lt;/div&gt;
        &lt;div class="info" id="2"&gt;Div 2&lt;/div&gt;
        &lt;div class="info" id="3"&gt;Div 3&lt;/div&gt;
        &lt;div class="info" id="4"&gt;Div 4&lt;/div&gt;
        &lt;div class="info" id="5"&gt;Div 5&lt;/div&gt;
        &lt;div class="info" id="6"&gt;Div 6&lt;/div&gt;
        &lt;div class="info" id="7"&gt;Div 7&lt;/div&gt;
        &lt;div class="info" id="8"&gt;Div 8&lt;/div&gt;
  
   &lt;button class="button" onclick="mostraDiv('inicio')" style="float:left"&gt;inicio&lt;/button&gt;
   &lt;button class="button" onclick="mostraDiv('avancar')" style="float:right"&gt;seguinte&lt;/button&gt;
   &lt;button class="button" onclick="mostraDiv('anterior')" style="float:right"&gt;retroceder&lt;/button&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>JS</p>
  
  <pre><code>var divNo = 0;
  function mostraDiv(direction) {
    var sel = document.getElementById('main').getElementsByTagName('div');
    for (var i=0; i&lt;sel.length; i++) { sel[i].style.display = 'none'; }
    switch (direction) {
      case 'inicio' : divNo = 0; break;
      case 'anterior' : divNo--;   break;
      case 'avancar' : divNo++;   break;
      case 'ultima' : divNo = sel.length-1; break;
  
    }
    if (divNo &gt; sel.length-1) { divNo = 0; }
    else { if (divNo &lt; 0) { divNo = sel.length-1; } }
    sel[divNo].style.display = 'block';
  }
  
  onload = function() {
    mostraDiv('s');
  };
  </code></pre>
  
  <p>This function i found here and works fine for me. </p>
  
  <p>Thanks in Advance.</p>